I am working with the pseudo random function srand()
to generate a number between a range of two integers (1 and 4).
I include ctime
and use a flag controlled while loop to check to see that the numbers will be different each loop.
time_t t;
time(&t);
srand(t);
and then print out the results
cout << "My random number" << (rand()%(1-5))+1 << endl;
I wish to replace (rand()%(1-5))+1
with a variable ex : int myRandomNumber
for use in another loop for determining how many objects will be generated later on in my code
How can i assign the desired random value i get to myRandomNumber
?
Thank you for your time,
int myRandomNumber = (rand() % 4) + 1;
Just declare the variable:
int myRandomNumber;
and then assign the value to the variable:
myRandomNumber=(rand()%(1-5))+1;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.