Problems with random number....

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 23
Reputation: gopi17 is an unknown quantity at this point 
Solved Threads: 0
gopi17 gopi17 is offline Offline
Newbie Poster

Problems with random number....

 
0
  #1
Aug 8th, 2009
can somebody help me with this....i'm creating a 4X4 sudoku program...with 8 auto-generating numbers and 8 user inputs...i used the random number function rand() but the numbers keep on repeating...the problem is i'm not allowed to use arrays...
  1. int main()
  2. {
  3. int row1col1,row1col3,row2col2,row2col4,row3col1,row3col3,row4col2,row4col4;
  4. int row1col2 = 0;
  5. int row1col4 = 0;
  6. int row2col1 = 0;
  7. int row2col3 = 0;
  8. int row3col2 = 0;
  9. int row3col4 = 0;
  10. int row4col1 = 0;
  11. int row4col3 = 0;
  12.  
  13. row1col1=((rand()%4)+1);
  14. row1col3=((rand()%4)+1);
  15. row2col2=((rand()%4)+1);
  16. row2col4=((rand()%4)+1);
  17. row3col1=((rand()%4)+1);
  18. row3col3=((rand()%4)+1);
  19. row4col2=((rand()%4)+1);
  20. row4col4=((rand()%4)+1);
  21.  
  22. char choice;
  23. bool done = false;
  24. do
  25. {
  26. displayMenu();
  27. cin >> choice;
  28. choice = toupper(choice);
  29. switch (choice)
  30. {
  31. case '1' : enterdata1(row1col2);
  32. displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  33. break;
  34. case '2' : enterdata2(row1col4);
  35. displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  36. break;
  37. case '3' : enterdata3(row2col1);
  38. displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  39. break;
  40. case '4' : enterdata4(row2col3);
  41. displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  42. break;
  43. case '5' : enterdata5(row3col2);
  44. displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  45. break;
  46. case '6' : enterdata6(row3col4);
  47. displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  48. break;
  49. case '7' : enterdata7(row4col1);
  50. displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  51. break;
  52. case '8' : enterdata8(row4col3);
  53. displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  54. break;
  55. case '9' : displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  56. break;
  57. case 'V': verifydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4);
  58. break;
  59. system("pause");
  60. case 'Q' : done = true;
  61. break;
  62. system("pause");
  63. default : cout << "Invalid selection, try again!" << endl;
  64. system("pause");
  65. break;
  66. }
  67. } while (!done);
  68.  
  69. cout << "Good Bye!" << endl;
  70. cout << endl;
  71.  
  72. return 0;
  73. system("pause");
  74. }

how do i use random number with a set of numbers without using array...is it possible..
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Problems with random number....

 
0
  #2
Aug 8th, 2009
Can you use pointers and dynamic memory?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Problems with random number....

 
2
  #3
Aug 8th, 2009
(rand()%4)+1 can only give you values from 1 to 4, so you're going to have duplicates, possibly several of the same.

To get unique values, at each assignment you have to examine the previously chosen numbers and reject any duplications as you create them.

Frankly, giving an assignment like this and not allowing array use - I'd slap the teacher. Wait, I am a teacher. Ok, speak very sternly to him/her.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Problems with random number....

 
0
  #4
Aug 8th, 2009
Originally Posted by gopi17 View Post
the problem is i'm not allowed to use arrays...
Why not? One can always get around this stuff by using a vector instead that functions as an array, but I imagine that's cheating. There must be some reason why you're not allowed to use an array. I don't know anyone would want you to use variables like row2col4 unless the point is to force you to do it the wrong way so you'll really appreciate the right way. I can think of any number of ways to do this, but the question remains "Why are you not allowed to use arrays?" Maybe you're studying linked lists or trees or whatever.
Last edited by VernonDozier; Aug 8th, 2009 at 2:22 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Problems with random number....

 
0
  #5
Aug 8th, 2009
Is that your class haven't learned arrays, or that the teacher
said you can't use arrays. How about emailing your teacher.

Btw, you need to seed your random number generator like so : srand(time(0));
Last edited by firstPerson; Aug 8th, 2009 at 3:02 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Problems with random number....

 
0
  #6
Aug 8th, 2009
How about using bit history!
  1. unsigned int nBits, b;
  2. int r;
  3.  
  4. srand( time(NULL) );
  5.  
  6. nBits = 0;
  7. while (nBits != (1 << 16)-1 )
  8. {
  9. r = rand() % 15; // 0...15
  10. b = 1 << r;
  11. if ( b ^ nBits) // bit not set
  12. {
  13. nBits |= b; // set bit, one more unique number found!
  14.  
  15. // Unique number is (r) so do your processing here!
  16. }
  17. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Problems with random number....

 
0
  #7
Aug 8th, 2009
rand() % 15; // ranges from 0 -> 14
wildgoose, would you mind explaining you snippet a little "bit" more?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 23
Reputation: gopi17 is an unknown quantity at this point 
Solved Threads: 0
gopi17 gopi17 is offline Offline
Newbie Poster

Re: Problems with random number....

 
0
  #8
Aug 8th, 2009
thank u for your help guys...really appreciate them...
i manage to solve them...i did something like this
  1. srand(1);
  2. {
  3. row1col1=((rand()%4)+1);
  4. row3col1=((rand()%4)+1);
  5. row1col3=((rand()%4)+1);
  6. row2col2=((rand()%4)+1);
  7. }
  8. srand(1);
  9. {
  10. row2col4=((rand()%4)+1);
  11. row4col4=((rand()%4)+1);
  12. row4col2=((rand()%4)+1);
  13. row3col3=((rand()%4)+1);
  14. }

broke them up into 2 and the switch the positions...basically not a random number..just a increment thing..haha..take care guys..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Problems with random number....

 
0
  #9
Aug 8th, 2009
Originally Posted by gopi17 View Post
thank u for your help guys...really appreciate them...
i manage to solve them...i did something like this
  1. srand(1);
  2. {
  3. row1col1=((rand()%4)+1);
  4. row3col1=((rand()%4)+1);
  5. row1col3=((rand()%4)+1);
  6. row2col2=((rand()%4)+1);
  7. }
  8. srand(1);
  9. {
  10. row2col4=((rand()%4)+1);
  11. row4col4=((rand()%4)+1);
  12. row4col2=((rand()%4)+1);
  13. row3col3=((rand()%4)+1);
  14. }

broke them up into 2 and the switch the positions...basically not a random number..just a increment thing..haha..take care guys..
You do realize that you'll get the same "random" numbers each time since you are hard-coding the seed value as 1. right?

Edit: Or is that a lower case L, not a hard-coded 1?
Last edited by VernonDozier; Aug 8th, 2009 at 2:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Problems with random number....

 
0
  #10
Aug 8th, 2009
Good catch 'firstPerson'. I originally had an AND & 15 but then since the original poster was using mod %16 I changed it to mod but forgot the divisor to 16.

As to your other question. It's a bit array. It's not an offical array[] so its a work around the "No Array Rule".

All bits are cleared. The while loop loops until all 16 bits are set since 16 unique numbers are required. The idea (though not efficient) is to generate a random number between 0 and 15, see if the bit is still clear. If so, then set the bit to indicate you have the new unique number and then use it. You then loop around and do it again. Simple! Merely using individual bits as flags.

---
I personally prefer a shuffle algorithm. This involves an array with sequential numbers in it. Randomize N (in this case N is 16). Then switch contents of array of index 0 with randomized index. index is now index + 1, and N is N-1. Repeat N-1 times and you now have a unique shuffle typically used by video poker machines in casinos!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 306 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC