- #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
also what type of random numbers are generated by rand & srand.?
thanks
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.
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
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.
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().
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.
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.