First, you need to initialize the position with the function time() which returns the elapsed time in seconds since January 1, 1970, otherwise the sequence generated would be the same each time.
We chose to generate numbers between two bounds, lower and upper. The numbers returned are part of the interval [a, b] where is the lower bound and b and the upper bound. Just draw a number and then multiply it by (b-a+1) and divide the total on RAND_MAX which is the maximum value returned by the function rand().
Output:
References:
Openclassrooms: randomness in C and C++
cplusplus.com: time.h
cplusplus.com: RAND_MAX
Commentaires (0)
Laisser un commentaire
Connectez-vous pour commenter
Rejoignez la discussion et partagez vos connaissances avec la communauté
Chargement des commentaires...