Mail Archives: djgpp/1997/03/05/05:04:03
Gunnar wrote:
>Hi!
>
>Sorry for asking that stupid question, but can anybody tell me how i can
>get random numbers from 0 to 255?
>
>Do i have to call something like a randomize timer function like in
>Borland?
>
>---
>Gunnar Beushausen
>GBeushausen AT t-online DOT de
>http://www.hof.de/~gbasic
Hi Gunnar,
To get random numbers try the following sample, it uses standard
C libraries so should be portable as well.
Mit freundlichen gruessen
John
-----> CUT HERE <-----
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
main()
{
int
random,
loop;
/*
* Randomise the seed used for random numbers
* the easiest way to do this is from the system clock
*/
srand(time(NULL));
for(loop = 0; loop < 20; loop++)
{
/* Get the random number */
random = rand() % 256;
printf("Random number %2d: %d\n", loop, random);
}
}
-----> CUT HERE <-----
________________________________________________________________
Parallax Solutions Ltd. Tel.: 01203 514522
Stonecourt, Fax.: 01203 514401
Siskin Drive, Web : http://www.parallax.co.uk/~johne
Coventry CV3 4FJ Mail: johne AT parallax DOT co DOT uk
________________________________________________________________
Remember:
There is no such thing as computer error, it's 100% YOUR fault
________________________________________________________________
- Raw text -