| | |
Problems with random number....
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 23
Reputation:
Solved Threads: 0
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...
how do i use random number with a set of numbers without using array...is it possible..
C++ Syntax (Toggle Plain Text)
int main() { int row1col1,row1col3,row2col2,row2col4,row3col1,row3col3,row4col2,row4col4; int row1col2 = 0; int row1col4 = 0; int row2col1 = 0; int row2col3 = 0; int row3col2 = 0; int row3col4 = 0; int row4col1 = 0; int row4col3 = 0; row1col1=((rand()%4)+1); row1col3=((rand()%4)+1); row2col2=((rand()%4)+1); row2col4=((rand()%4)+1); row3col1=((rand()%4)+1); row3col3=((rand()%4)+1); row4col2=((rand()%4)+1); row4col4=((rand()%4)+1); char choice; bool done = false; do { displayMenu(); cin >> choice; choice = toupper(choice); switch (choice) { case '1' : enterdata1(row1col2); displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; case '2' : enterdata2(row1col4); displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; case '3' : enterdata3(row2col1); displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; case '4' : enterdata4(row2col3); displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; case '5' : enterdata5(row3col2); displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; case '6' : enterdata6(row3col4); displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; case '7' : enterdata7(row4col1); displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; case '8' : enterdata8(row4col3); displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; case '9' : displaydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; case 'V': verifydata(row1col1,row1col2,row1col3,row1col4,row2col1,row2col2,row2col3,row2col4,row3col1,row3col2,row3col3,row3col4,row4col1,row4col2,row4col3,row4col4); break; system("pause"); case 'Q' : done = true; break; system("pause"); default : cout << "Invalid selection, try again!" << endl; system("pause"); break; } } while (!done); cout << "Good Bye!" << endl; cout << endl; return 0; system("pause"); }
how do i use random number with a set of numbers without using array...is it possible..
(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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
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.
How about using bit history!
C++ Syntax (Toggle Plain Text)
unsigned int nBits, b; int r; srand( time(NULL) ); nBits = 0; while (nBits != (1 << 16)-1 ) { r = rand() % 15; // 0...15 b = 1 << r; if ( b ^ nBits) // bit not set { nBits |= b; // set bit, one more unique number found! // Unique number is (r) so do your processing here! } }
•
•
Join Date: Sep 2008
Posts: 23
Reputation:
Solved Threads: 0
thank u for your help guys...really appreciate them...
i manage to solve them...i did something like this
broke them up into 2 and the switch the positions...basically not a random number..just a increment thing..haha..take care guys..
i manage to solve them...i did something like this
C++ Syntax (Toggle Plain Text)
srand(1); { row1col1=((rand()%4)+1); row3col1=((rand()%4)+1); row1col3=((rand()%4)+1); row2col2=((rand()%4)+1); } srand(1); { row2col4=((rand()%4)+1); row4col4=((rand()%4)+1); row4col2=((rand()%4)+1); row3col3=((rand()%4)+1); }
broke them up into 2 and the switch the positions...basically not a random number..just a increment thing..haha..take care guys..
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
thank u for your help guys...really appreciate them...
i manage to solve them...i did something like this
C++ Syntax (Toggle Plain Text)
srand(1); { row1col1=((rand()%4)+1); row3col1=((rand()%4)+1); row1col3=((rand()%4)+1); row2col2=((rand()%4)+1); } srand(1); { row2col4=((rand()%4)+1); row4col4=((rand()%4)+1); row4col2=((rand()%4)+1); row3col3=((rand()%4)+1); }
broke them up into 2 and the switch the positions...basically not a random number..just a increment thing..haha..take care guys..
Edit: Or is that a lower case L, not a hard-coded 1?
Last edited by VernonDozier; Aug 8th, 2009 at 2:47 pm.
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!
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!
![]() |
Similar Threads
- Random Number Problem For Lottery Program (Visual Basic 4 / 5 / 6)
- random number display the same (C#)
- Random Number Generator Question (Java)
- Random number generator's (C++)
- Need aome help in generating a Random Number (C++)
- Random number with ascending order help (C++)
- Help with random number gen (C++)
Other Threads in the C++ Forum
- Previous Thread: Problem Of tanks
- Next Thread: c/c++ sockets - read from server
Views: 304 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






