Thursday, July 9, 2009

How can I generate a random number between 0-6 using c++?

use the rand function


if you want a different number everytime use srand to seed the random number based off time





srand(time(0));


int number = rand %7;





hope this helps

How can I generate a random number between 0-6 using c++?
To generate a random integer in C (which is still usable in unmanaged C++):


rand()%7





the function rand() (requires header %26lt;stdlib.h%26gt;) returns a random integerand the modulus (%) operator limits the resulting number between 0-6. (the modulus gets the remainder of the division which is between 0-6)





There's also a safer version called rand_s





Use System::Random class for managed C++


No comments:

Post a Comment