Random funtion???



Do you know are there any random funtions in c ?
if yes, how can i use it?(rand () funtion)
You should call srand(time(0)) at the beginning of your program to set the random seed (only once)
Then, any time you want a random number call rand() which returns a number from 0 to RAND_MAX.
If you want a range from 0 to n-1, use rand()%n
Most would recommend random() and srandom(seed) over rand and srand. It works exactly the same way. Just include stdlib.h and seed with srandom(time(0)).
If you are on a windows platform, the best way (without downloading additional libraries) is to use rand_s(). The differences between the different functions listed her would be negligible in most cases though, I'm sure any of these will be fine for your needs.
Those are not in the standard C library.
rossipoo: No idea what stdlib.h you're looking at, but those functions are definitely not in the C89 standard library.

Tevsky: Why introduce unportable functions when something different would fit the need perfectly?

thank you so much for your answers...
Topic archived. No new replies allowed.