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

  • Thread starter the_kool_guy
  • Start date
  • Tags
    Sleep
In summary, the C library provides the functions rand and srand for generating pseudorandom numbers within a specified range. The sleep function, although not a standard C library function, is available in GNU libc and can be used for delaying program execution. The rand function returns an integer between 0 and RAND_MAX, while srand is used for seeding the pseudorandom number generator. To generate a constantly changing seed, additional code must be implemented. Overall, these functions are useful for tasks such as studying the results of a dice roll or creating precise results through repeated function calls.
  • #1
the_kool_guy
37
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
  • #2
Last edited by a moderator:
  • #3
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:

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

What is the purpose of rand() in C?

The rand() function in C is used to generate a random number. It takes no parameters and returns an integer value between 0 and RAND_MAX, which is a constant defined in the header file.

How does srand() work in C?

The srand() function in C is used to initialize the random number generator. It takes an integer value as a seed, which is used to generate a sequence of random numbers. If the same seed is used, the same sequence of numbers will be generated. Therefore, it is recommended to use a different seed each time the program is run to get different sequences of random numbers.

What is the difference between rand() and srand() in C?

The rand() function generates a random number, while the srand() function initializes the random number generator. In order to get a different sequence of random numbers each time the program is run, srand() should be used to set a different seed value before calling rand().

How can I use rand() to generate a random number in a specific range?

To generate a random number in a specific range, you can use the modulus operator (%) with the return value of rand(). For example, if you want to generate a random number between 1 and 10, you can use rand() % 10 + 1. This will give you a random number between 0 and 9, and by adding 1, the range becomes 1-10.

What is the purpose of the sleep() function in C?

The sleep() function in C is used to suspend the execution of the program for a specified number of seconds. It takes an integer value as a parameter, representing the number of seconds to sleep for. This can be useful for creating delays or timing certain processes in a program.

Similar threads

Replies
4
Views
1K
Replies
8
Views
3K
Replies
9
Views
2K
Replies
3
Views
3K
Replies
9
Views
2K
Replies
9
Views
7K
Back
Top