View Single Post
Join Date: Nov 2008
Posts: 8
Reputation: techgenie is an unknown quantity at this point 
Solved Threads: 0
techgenie techgenie is offline Offline
Newbie Poster

Re: Generate 6 Random Numbers

 
0
  #6
Nov 14th, 2008
  1. using std::cout;
  2. using std::cin;
  3. using std::endl;
  4.  
  5. #include <string>
  6. using std::string;
  7.  
  8. #include <cstdlib>
  9. using std::rand;
  10. using std::srand;
  11.  
  12. #include <ctime>
  13. using std::time;
  14.  
  15. #include "lottery.h" // include definition of class lottery
  16.  
  17. // constructor for the Lottery
  18. Lottery::Lottery()
  19. {
  20.  
  21. } // end of Lottery constructor
  22.  
  23. bool Lottery::checkLottery(int lotnum, int lottery_numbers[6])
  24. {
  25.  
  26. for (int i=0; i < 6; i++)
  27. {
  28. lottery_numbers[i] = i * i; //store lottery numbers in the index
  29. if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0)
  30. {
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. void Lottery::setLottery(int lottery_numbers[6])
  37. {
  38. lotNumbers[6] = lottery_numbers[6];
  39. }
  40.  
  41. int Lottery::getLottery()
  42. {
  43. return lotNumbers[6];
  44. int lot_count = 0; // counter for the number of valid lottery numbers found
  45. while (lot_count < 6)
  46. {
  47. time_t t;
  48. srand(unsigned (time(&t)));
  49. int lotnum = 1 + rand() % 53;
  50. if (checkLottery(lotnum, lotNumbers))
  51. {
  52. lotNumbers[lot_count] = lotnum;
  53. lot_count++;
  54. }
  55. }
  56. }
  57.  
  58. void Lottery::displayLottery(int [])
  59. {
  60. getLottery();
  61. for (int j = 0; j < 6; j++)
  62. {
  63. cout << getLottery() << " " << endl;
  64. }
  65. }

Hope this helps!
Reply With Quote