the question given like this
The TetraPek manager assumed that you are already familiar with the game rules. Now, the
manager wants your program to be able to auto-generate some numbers before the player gets to
key in their numbers into the empty boxes. It is up to you how these starting numbers are generated.
Among some ideas you can consider: You can have preset locations for preset numbers, or you can
also randomly generate the numbers and positions for more playing fun.
Note that each row, each column and each quadrant (4 boxes) will only have two auto-generated
numbers. The important part is to make sure that the numbers generated are within the range from 1
to 4, and also fulfill all game rules. If not, the game might be unplayable!
The following grid is an example of how the starting numbers can be generated

 --------------------------------------------------------------------
|         3        |                      |         4           |                  |

---------------------------------------------------------------------

|                   |           1         |                      |        2        |

----------------------------------------------------------------------

|                   |           4         |                      |        3        |
----------------------------------------------------------------------

|    2             |                      |           1         |                  |
----------------------------------------------------------------------

There should be eight (8) auto-generated numbers (ranging from 1 to 4) at the beginning of thegame.
Next, allow the user to enter numbers for the empty boxes into your program. The user shouldhave the choice of selecting which box he/she wants to fill in. This approach is user-friendly
since the user no longer needs to fill in following a particular rigid order.

i have done with that game .im having problem with generate random numbers on mentioned position on above box .
i tried a bit with that function .but it didnt work .

intialize box function ;

void crate game ()
{
  for(int i=1 ;i<=4 ;i++)
 {

             C11=random(i);
             C12=' ';
             C13=random(i);
             C14=' ';
             C21=random(i);
             C22=' ';
             C23=' ';
             C24=random(i);
             C31=' ';
             C32=random(i);
             C33=' ';
             C34=random(i);
             C41=random(i);
             C42=' ';
             C43=random(i);
             C44=' ';

my random function  "

int random (int n){
 for(int i=1;i<=4;i++)
{
    i =rand();
  return i;

}

}

can any one help me to finish this ?

Recommended Answers

All 6 Replies

Your function generates a random number in the range of [0..RAND_MAX), not in the range of [1..5). The easiest trick for fixing the range is the remainder operator:

i = rand() % 4 + 1;

The remainder of division from RAND_MAX by 4 will always result in a value in the range of [0..4), or 0, 1, 2, and 3. Adding 1 to that shifts the range to [1..5). The easiest trick might not be the best trick if rand() does not have good randomization of low order bits.

The random() function should probably do something like this, with whatever random trick you want:

int random(int lb, int ub)
{
    return std::rand() % (ub - lb) + lb;
}

Then in the calling function, call random() in a loop 8 times:

for (int x = 0; x < 8; ++x) cout << random(1, 5) << '\n';

thank you very much for your help .

You appear to need unique numbers but you are generating non-unique numbers.

You have a 2x4, unique numbers in the upper 2x2 and another unique set in the lower unique 2x2. So generate the top set, then the bottom set.

You can shuffle four numbers 1...4 and then insert them, and then do the same for the bottom four numbers.
An array of 4
Sequentially set them
Loop 3 times index 0 to 2 randomize 4 numbers
swap index 0 with that number
index++
randomize 3 numbers
swap index 1 with 1+rand
index++
randomize 2 numbers
swap index 2 with 2+rand
done

Now you have four unique randomized numbers in the array.

Now do it again for bottom 2x2.

Here is another random generator :

int  randI( int min , int max)
{ 
    return    (float)rand()/RAND_MAX * (max-min) + min;
}

since generating 1 from (float)rand()/RAND_MAX has low odds, thus returning max would be low, so to fix it
you can shift the odds by adding 0.1f;

so the function would look like this :

int  randI( int min , int max)
{ 
    return    ( 0.1f + (float)rand()/RAND_MAX  ) * (max-min) + min;
}

since generating 1 from (float)rand()/RAND_MAX has low odds

I am not good at taking people's word. ;) Why does (float)rand()/RAND_MAX have low odds of generating 1 and how does it justify adding .1 in all cases?

I am not good at taking people's word. ;) Why does (float)rand()/RAND_MAX have low odds of generating 1 and how does it justify adding .1 in all cases?

It has low odds of generating 1 because for rand()/RAND_MAX to be
1, rand() HAS to generate RAND_MAX in which the odds are
1 out of 32767(RAND_MAX). Percentage wise its 0.000031.

In using 0.1f, it shifts the chances by a little bit more.

When (float)rand()/RAND_MAX generates 0.9 through 1.0, adding
0.1 would make it range from 1.0 to 1.1. This result gets multiplied to max-min, and since its an int it would not cause this function to
generate a higher number than max, unless the offset if set to
0.5 instead of 0.1, then it has a chance.

So the chances increases because now rand has to only generate
a number greater than 0.9 * RAND_MAX to get a 1. And that chance
from my calculation leads to 0.10 percent of generating maxNum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.