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
  #4
Nov 14th, 2008
Thank you skatamatic for that suggestion on the modulus (%). Narue I realized I had omitted the <ctime> and <cstdlib> libraries. After adding them it made it easier for me to identify the other errors.

As the program stands right now; it has finally compiled but I do not get random numbers between 1 and 53, as it should. I am getting an extremely large number out of range
(- 858993460). I get 6 of the same numbers.
Any suggestions?

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