424,652 Members | 1,487 Online
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

# C++ how to execute for loop and generate a random number??

 P: 6 I have to write a C++ program that picks a random number between 0 and 49. If the number is even lets say 30, then the computer will display 30, 32, 34, 36... all the way till 100, if its odd lets say 17, then the computer will display 17, 19.. till 99. I got the computer picking a random number, I just can't figure out how to display every other number using a for loop statement. Here's my code for random number generator: Expand|Select|Wrap|Line Numbers     #include  #include   #include    using namespace std;   int main()   {       {       srand((unsigned)time(0));       int random_integer;       random_integer = (rand()%49);       cout << random_integer << endl;       }         return 0; }        Oct 24 '14 #1
4 Replies

 Expert 100+ P: 2,391 I believe rand()%49 yields a value between 0 and 48. Oct 24 '14 #2

 Expert Mod 5K+ P: 9,197 Just write a loop that adds 2 to your number, displays the number, and keeps going while the number is less than or equal to 100. Oct 24 '14 #3

 P: 6 I know the logic behind it but I just can't get started on how to write the loop Oct 24 '14 #4

 Expert Mod 5K+ P: 9,197 Expand|Select|Wrap|Line Numbers while(put your test here) {    ...add 2 to your number    ...display the number } As a for loop it looks like: Expand|Select|Wrap|Line Numbers for(; put your test here; ) {    ...add 2 to your number    ...display the number } Oct 24 '14 #5