Stopping same number from appearing twice?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Stopping same number from appearing twice?

 
0
  #1
Jun 21st, 2008
Hey, my dad was checking the "lotto" results so that inspired me to make a little program of my own, i accomplished that just got a problem..

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. srand(time(NULL));
  8.  
  9. unsigned int lotteryBall;
  10.  
  11. for(int i = 0; i <=7; ++i)
  12. {
  13. lotteryBall = rand() % 48 + 1;
  14.  
  15. cout << lotteryBall << " ";
  16.  
  17. if(i == 7)
  18. cout << "*" << lotteryBall << "*";
  19. }
  20.  
  21. cin.get();
  22.  
  23. return 0;
  24. }

I compiled and ran the code and got : 13 19 20 9 31 12 37 46 *46*

But obviously you can't have 46 twice, so I'm not sure how I could change it.

And before you say "the numbers need to go in order from smallest to largest" I will add bubble sort after I have got the main problem solved.

Many thanks.
Last edited by Black Magic; Jun 21st, 2008 at 6:10 am.
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,459
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Stopping same number from appearing twice?

 
0
  #2
Jun 21st, 2008
The reason you got 46 twice there isn't because the random number happends to be the same, its because you are printing the same variable twice without modifying it. If you want to make it so each number can only come up once, you have to make an array for the numbers to go into it and then check it to see if it has already been added.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 68
Reputation: Tigran is an unknown quantity at this point 
Solved Threads: 6
Tigran Tigran is offline Offline
Junior Poster in Training

Re: Stopping same number from appearing twice?

 
0
  #3
Jun 21st, 2008
I've modified the script a bit

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. srand(time(NULL));
  8.  
  9. unsigned int lotteryBall;
  10.  
  11. for(int i = 0; i <=7; ++i)
  12. {
  13. lotteryBall = rand() % 48 + 1;
  14.  
  15. cout << lotteryBall << " ";
  16.  
  17. if(i == 7)
  18. lotteryBall = rand() % 48 + 1;
  19. cout << "*" << lotteryBall << "*";
  20. }
  21.  
  22. cin.get();
  23.  
  24. return 0;
  25. }

just added 1 more line
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,459
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Stopping same number from appearing twice?

 
0
  #4
Jun 21st, 2008
That wont work, you fergot to add curly brackets for the if statement.
Last edited by William Hemsworth; Jun 21st, 2008 at 9:25 am.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 68
Reputation: Tigran is an unknown quantity at this point 
Solved Threads: 6
Tigran Tigran is offline Offline
Junior Poster in Training

Re: Stopping same number from appearing twice?

 
0
  #5
Jun 21st, 2008
how can i edit my post?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,459
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Stopping same number from appearing twice?

 
0
  #6
Jun 21st, 2008
You can only edit your post within 30 minutes of posting it, you will just have to repost it with the correct changes.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: Stopping same number from appearing twice?

 
0
  #7
Jun 21st, 2008
Is this right?
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. srand(time(NULL));
  8.  
  9. unsigned int lotteryBall[9];
  10.  
  11. for(int i = 0; i <=7; ++i)
  12. {
  13. lotteryBall[i] = rand() % 48 + 1;
  14.  
  15. cout << lotteryBall[i] << " ";
  16.  
  17. if(i == 7)
  18. { do {
  19. lotteryBall[i+1] = rand() % 48 + 1;}
  20. while (
  21. (lotteryBall[i+1]==lotteryBall[0])||
  22. (lotteryBall[i+1]==lotteryBall[1])||
  23. (lotteryBall[i+1]==lotteryBall[2])||
  24. (lotteryBall[i+1]==lotteryBall[3])||
  25. (lotteryBall[i+1]==lotteryBall[4])||
  26. (lotteryBall[i+1]==lotteryBall[5])||
  27. (lotteryBall[i+1]==lotteryBall[6])||
  28. (lotteryBall[i+1]==lotteryBall[7]));
  29.  
  30. cout << "*" << lotteryBall[i+1] << "*"; }
  31. }
  32.  
  33. cin.get();
  34.  
  35. return 0;
  36. }

Probably much more efficient way that it can be done
Last edited by salman213; Jun 21st, 2008 at 3:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,459
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Stopping same number from appearing twice?

 
0
  #8
Jun 21st, 2008
yep.. try learning loops salman
Last edited by William Hemsworth; Jun 21st, 2008 at 3:28 pm.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: Stopping same number from appearing twice?

 
0
  #9
Jun 21st, 2008
would this be better or worse ???

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int check (unsigned int checkvariable[]);
  6.  
  7. int main()
  8. {
  9. srand(time(NULL));
  10.  
  11. unsigned int lotteryBall[9];
  12.  
  13. for(int i = 0; i <=7; ++i)
  14. {
  15. lotteryBall[i] = rand() % 48 + 1;
  16.  
  17. cout << lotteryBall[i] << " ";
  18.  
  19. if(i == 7)
  20. {
  21. do {
  22. lotteryBall[i+1] = rand() % 48 + 1;
  23. }while (check(lotteryBall));
  24. cout << "*" << lotteryBall[i+1] << "*";
  25. }
  26.  
  27. }
  28.  
  29. cin.get();
  30.  
  31. return 0;
  32. }
  33.  
  34. int check ( unsigned int checkvariable[])
  35. {
  36. for(int a=0;a<=7;++a)
  37. {
  38. if (checkvariable[a]==checkvariable[8])
  39. {return true;
  40. break;}
  41. else if (a==7)
  42. return false;
  43. }
  44.  
  45. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 53
Reputation: Ellisande is an unknown quantity at this point 
Solved Threads: 2
Ellisande Ellisande is offline Offline
Junior Poster in Training

Re: Stopping same number from appearing twice?

 
0
  #10
Jun 21st, 2008
I thought you needed to #include <cstdlib> in order to use the rand() function...?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC