I need code to create function for the random selection between two numbers, I looked on the internet for examples of uses, but I did not understand much, but what l want know is that how to use the random number in c program.
Printable View
I need code to create function for the random selection between two numbers, I looked on the internet for examples of uses, but I did not understand much, but what l want know is that how to use the random number in c program.
Try this :
Code:int random (int min, int max) (
return (int) (min + ((float) rand () / RAND_MAX * (max - min + 1)));
)
The key function in generating random numbers is; int random (int n); which generates a random number in the range of 0 to n-1. Random number generators (pseudo-random, of course) in C; mostly fast one-liners for "inline" use (to eliminate call / return overhead). "srand()/rand()" isn't exactly the most sophisticated random number generator in the world.
The random number generator produces a sequence of numbers that gives the appearance of being random, when in fact the sequence will eventually repeat and is predictable. Generating random numbers in C is needed in several applications. For instance, generating a default password for a new user on a network uses random generation of numbers and letters.