# "Deal or No Deal" Game

• 12-05-2006
jaromdl
"Deal or No Deal" Game
Okay. This is my first project I am doing. I'm creating a simple console app that is not unlike the "Deal or No Deal" television show.

I have two sets of 26 different numbers that I would like to show.

One of them containing the case numbers.

The other containing what is inside the case.

\$0.01
\$1
\$5
\$10
\$25
\$50
\$75
\$100
\$200
\$300
\$400
\$500
\$750

(split)

\$1,000
\$5,000
\$10,000
\$25,000
\$50,000
\$75,000
\$100,000
\$200,000
\$300,000
\$400,000
\$500,000
\$750,000
\$1,000,000

The plan is to randomly assign one of the 26 cases to one of the 26 values. I am not exactly sure how to do this, and I was wondering if anyone would like to help.

I didn't really understand pointers, but is this where I would use them?

Cheers!
• 12-05-2006
SlyMaelstrom
You can do this without pointers. In fact, you can do this with nothing more than an array and the rand function. Give it a try and tell us what you get.
• 12-05-2006
jaromdl
Thanks for the hint. :) I'll think about it.
• 12-05-2006
SlyMaelstrom
You can search the FAQs here if you need help using the rand function. Your most difficult task would be figuring out the algorithm you'll be using to decide how much money the bank offers after each round.
• 12-05-2006
jaromdl
Quote:

Originally Posted by SlyMaelstrom
You can search the FAQs here if you need help using the rand function. Your most difficult task would be figuring out the algorithm you'll be using to decide how much money the bank offers after each round.

Yeah, I'm taking it in steps. I read something about array shuffling.

Here is what I got (well just a part of it)...

Code:

```   //--- Shuffle elements by randomly exchanging each with one other.         for (int i=0; i<26; i++) {             int r = rand() % 26;  // generate a random position             int temp = case[i]; case[i] = case[r]; case[r] = temp;```
...and then manually, or find a way to automatically, assign each of those case numbers to one of the values. After that, use an algorithm to re-sort the cases.
• 12-05-2006
jaromdl
Scratch that. I'm confusing myself.
• 12-05-2006
jaromdl
Okay, I'm not as confused anymore.
• 12-05-2006
swgh
You could also use a switch statement to identify the box number. Use the random seed generator to decide on a box to open. But this is only one of a few ways.

Code:

```for ( int open = 1; open <= 26' open++ ) {   box = rand() % 26 + 1; // variable to choose box to open   switch ( box )   {   case 1:   // box  contains... randomise this also   break;   }; }```
• 12-05-2006
Daved
The best way to randomly assign the values to the boxes is to put the values into a container in order, then randomly shuffle the data in the container.

So put each value into a vector (or array). Then call random_shuffle to shuffle the data.

If you can't or don't want to use the random_shuffle function, then you can simulate it yourself. Basically, you start from the end, then randomly pick an index in the vector/array and swap the value at that index with the value at the end. Then you decrement your counter by one and do it again with the smaller range. You keep doing this until you get to the front of the container and it will be completely shuffled. Be careful, it is easy to get the algorithm wrong and not get good randomness, which is why letting random_shuffle do it for you is better. If you do it yourself, feel free to post your attempt so we can help you verify that it is correct.
• 12-05-2006
QuestionC
Close. Very close.

The shuffle algorithm basically works with the following logic...
• Randomly select one of the 26 elements.
• Seperate it
• Randomly select one of the remaining 25 elements.
• Seperate it.
• Randomly select one of the remaining 24 elements.

'Seperating' basically amounts to sticking it at the end of the array, where it will not be selected for the next random roll.

Code:

```  for (int i= 0; i < 26; ++i) {       // With each successive step, the number of unshuffled elements       // decreases, so our random range decreases.       int r = rand() % (26 - i);       // Stick the randomly selected element at the end of the array,       // where it can't be selected in the next iteration.       int temp = case[(26 - 1) - i];       case[(26 - 1) - i] = case[r];       case[r] = case[(26 - 1) - i];```
A few points... they're sort of homework-y, but really, this is an algorithm to understand.
• What happens when r = 26 - i?
• What happens when i = 25?
• There's really no reason this algorithm should use the back of the array for swapping instead of the front (the definition of 'r' would have to change). However, there's no difference since the result is random.

Quote:

Originally Posted by jaromdl
Yeah, I'm taking it in steps. I read something about array shuffling.

Here is what I got (well just a part of it)...

Code:

```   //--- Shuffle elements by randomly exchanging each with one other.         for (int i=0; i<26; i++) {             int r = rand() % 26;  // generate a random position             int temp = case[i]; case[i] = case[r]; case[r] = temp;```
...and then manually, or find a way to automatically, assign each of those case numbers to one of the values. After that, use an algorithm to re-sort the cases.

• 12-05-2006
Daved
>> There's really no reason this algorithm should use the back of the array for swapping instead of the front.
That suggestion is for convenience as it makes the code easier to follow IMO.
• 12-05-2006
System_159
for reference, this is the shuffle algorithm I used in my poker game(changed to use integers):

Code:

```#include <ctime> #include <cstdlib> void shuffle() {         int tempNum;         srand( time(NULL) ); // Seeds the random number generator with the current Time         for(int j = 0; j < size; j++) // loop through the array once         {                 int k = rand() % size; //Produces a random integer number between 0 and size                 tempNum = yourArray[k];                // Pick an int at random                 yourArray[k] = yourArray[j]; // swaps the random int and the current int in the loop                 yourArray[j] = tempNum;         }         return; }//! End of shufle Definition```
• 12-05-2006
Daved
Unfortunately, that gives an uneven distribution. Try running it a thousand times times with a size of 5 and see how often each value gets assigned to each spot. They should be roughly even for a good shuffling algorithm.
• 12-05-2006
Daved
BTW, I wrote a simple test program to show the uneven distribution compared to random_shuffle which works pretty well:
Code:

```#include <iostream> #include <algorithm> #include <ctime> #include <cstdlib> void shuffle(int data[], int size) {     int tempNum;     for(int j = 0; j < size; j++) // loop through the array once     {         int k = std::rand() % size; //Produces a random integer number between 0 and size         tempNum = data[k];        // Pick an int at random         data[k] = data[j]; // swaps the random int and the current int in the loop         data[j] = tempNum;     }     return; }//! End of shufle Definition void run_test(bool use_random_shuffle) {     const int size = 5;     const int iterations = 100000;     int tallies[size][size] = { };     if (use_random_shuffle)         std::cout << "Using random_shuffle:\n";     else         std::cout << "Using shuffle:\n";     for (int i = 0; i < iterations; ++i)     {         int data[size];         for (int j = 0; j < size; ++j)             data[j] = j;         if (use_random_shuffle)             std::random_shuffle(data, data + size);         else             shuffle(data, size);         for (int j = 0; j < size; ++j)             ++tallies[j][data[j]];     }     for (int i = 0; i < size; ++i)     {         std::cout << i << ": ";         for (int j = 0; j < size; ++j)         {             std::cout << tallies[i][j] << ' ';         }         std::cout << '\n';     }     std::cout << '\n'; } int main() {     std::srand( static_cast<unsigned>(std::time(0)) );     run_test(true);     run_test(false);     run_test(false);     run_test(true); }```
One example of my output:
Code:

```Using random_shuffle: 0: 19941 19970 20079 19976 20034 1: 19985 19953 20130 19981 19951 2: 19828 20213 19943 20145 19871 3: 20207 20018 19859 19977 19939 4: 20039 19846 19989 19921 20205 Using shuffle: 0: 19991 24204 20765 18440 16600 1: 20029 18017 23039 20659 18256 2: 19983 18552 17648 22809 21008 3: 20020 19190 18504 17836 24450 4: 19977 20037 20044 20256 19686 Using shuffle: 0: 19723 24367 21115 18393 16402 1: 19965 17808 23249 20619 18359 2: 20258 18419 17323 22965 21035 3: 19944 19290 18527 17978 24261 4: 20110 20116 19786 20045 19943 Using random_shuffle: 0: 19943 20161 19959 19982 19955 1: 19924 20078 20048 19866 20084 2: 20176 19971 19980 19875 19998 3: 19836 19823 20148 20081 20112 4: 20121 19967 19865 20196 19851```
Notice that 4 (the last column) only comes in the 0 slot about 16.5% of the time, but it comes up in the 3 slot over 24% of the time with System_159's version (which is a very common mistake when trying to implement this algorithm). With random_shuffle they all will average about 20%.