954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problems with random number....

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...

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..

gopi17
Junior Poster
100 posts since Sep 2008
Reputation Points: 18
Solved Threads: 3
 

Can you use pointers and dynamic memory?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

(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. :twisted:

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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));

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

How about using bit history!

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

rand() % 15; // ranges from 0 -> 14
wildgoose, would you mind explaining you snippet a little "bit" more?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

thank u for your help guys...really appreciate them...
i manage to solve them...i did something like this

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..

gopi17
Junior Poster
100 posts since Sep 2008
Reputation Points: 18
Solved Threads: 3
 

thank u for your help guys...really appreciate them... i manage to solve them...i did something like this

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..

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?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You