943,769 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 500
  • C++ RSS
Aug 8th, 2009
0

Problems with random number....

Expand Post »
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...
C++ Syntax (Toggle Plain Text)
  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..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gopi17 is offline Offline
39 posts
since Sep 2008
Aug 8th, 2009
0

Re: Problems with random number....

Can you use pointers and dynamic memory?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 8th, 2009
2

Re: Problems with random number....

(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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Aug 8th, 2009
0

Re: Problems with random number....

Click to Expand / Collapse  Quote originally posted by gopi17 ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,373 posts
since Jan 2008
Aug 8th, 2009
0

Re: Problems with random number....

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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 8th, 2009
0

Re: Problems with random number....

How about using bit history!
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 8th, 2009
0

Re: Problems with random number....

rand() % 15; // ranges from 0 -> 14
wildgoose, would you mind explaining you snippet a little "bit" more?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 8th, 2009
0

Re: Problems with random number....

thank u for your help guys...really appreciate them...
i manage to solve them...i did something like this
C++ Syntax (Toggle Plain Text)
  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..
Reputation Points: 10
Solved Threads: 0
Light Poster
gopi17 is offline Offline
39 posts
since Sep 2008
Aug 8th, 2009
0

Re: Problems with random number....

Click to Expand / Collapse  Quote originally posted by gopi17 ...
thank u for your help guys...really appreciate them...
i manage to solve them...i did something like this
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,373 posts
since Jan 2008
Aug 8th, 2009
0

Re: Problems with random number....

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!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem Of tanks
Next Thread in C++ Forum Timeline: c/c++ sockets - read from server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC