How Do rand, srand, and sleep Functions Work in C Programming?

  • Thread starter Thread starter the_kool_guy
  • Start date Start date
  • Tags Tags
    Sleep
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 7K views
the_kool_guy
Messages
37
Reaction score
0
in C how can one use rand,srand and sleep functions...
also what type of random numbers are generated by rand & srand.?

thanks
 
Physics news on Phys.org
MisterX said:
sleep is not a C standard library function, but it is in GNU libc. http://www.gnu.org/software/libc/manual/html_mono/libc.html#Sleeping"

rand will typically return a pseudorandom integer in the range 0 to RAND_MAX. srand is for seeding the pseudorandom number generator.


Never heard of sleep function before!

rand will return a integer in the range 0 to RAND_MAX in the form of for example:

int rnumber;
rnumber=rand()%6; ---->this will give you a random number between 0 and 5
rnumber=rand()%6+1; ---->this will give you a random number between 0 and 6

This is great, the problem with this function alone is that it just gives you back a number of a already non-changing seed, if I am not mistaken!
To constantly change the seed you need to implement something in the beginning which i don't really remember what it is!

Also srand() function is best for let's say, a study on the results of a average number that gets out of a dice!

I was writing one program to make that several times by calling a function a several number of times to get a very precise result!

But I am rusty in C languagem although I am studying a little this vacations!
 
Last edited by a moderator: