2

I tried a lot but could not get a solution for this problem

Function returns numbers in range [1,6] with equal probability. You can use library's rand() function and you can assume implementation of rand() returns number in range number in range [0,RAND_MAX] with equal probability.

Geobits
  • 22,218
  • 6
  • 59
  • 103
user3187111
  • 35
  • 1
  • 5

2 Answers2

6

We'll do this in multiple steps.

You need to generate a number in the range [1, 6], inclusive.

You have a random number generator that will generate numbers in the range [0..RAND_MAX].

Let's say you wanted to generate numbers in the range [0..5]. You can do this:

int r = rand();  // gives you a number from 0 to RAND_MAX
double d = r / RAND_MAX;  // gives you a number from 0 to 1
double val = d * 5; // gives you a number from 0 to 5
int result = round(d);  // rounds to an integer

You can use that technique to So given a range of [0, high], you can generate a random number, divide by RAND_MAX, multiply by high, and round the result.

Your range is [1, 6], so you have to add another step. You want to generate a random number in the range [0, 5], and then add 1. Or, in general, to generate a random number in a given range, [low, high], you write:

int r = rand();
double d = r / RAND_MAX;
int range = high - low + 1;
double val = d * range;
result = round(val);

Obviously you can combine some of those operations. I just showed them individually to illustrate.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • 1
    Note that from pigeon hole principle - this will suffer from the same bias issue as `rand() % 6 + 1`, you are going to spread `MAX_RAND` numbers (pigeons) to 6 integers (pigeon holes) - this will result in one of the holes at least have `ceil(RAND_MAX/6)`, and assuming RAND_MAX%6 != 0 - it will be biased. Nevertheless - elegant! – amit Jan 15 '14 at 12:47
  • @amit you are correct . we should bind r in [1,rand_num],such that rand_num is closest to RAND_MAX and rand_num%6=0. – user3187111 Jan 15 '14 at 16:26
  • @Nik-Lz Dividing by `RAND_MAX` gives you a number in the range [0..1) (greater than or equal to 0, and less than 1.0). This will give a slightly skewed probability if `RAND_MAX%6 != 0`, as noted in the first comment. See the second comment for how to fix that. – Jim Mischel Jul 23 '18 at 17:49
  • 1
    @Nik-Lz We have to assume that `rand()` is returning numbers with equal probability. All we do is map those numbers to the given range, maintaining that probability. If you can't depend on `rand()` to do the right thing, then we'd have to write our own pseudo random number generator. That clearly wasn't part of the OP's question. – Jim Mischel Jul 23 '18 at 19:40
4

Basically you are looking for using the operator% (modolus).

r = rand() % 6 + 1

If you are afraid that RAND_MAX % 6 != 0 and the solution will be biased - you just need to 'throw' some numbers (up to 5) out and redraw if you get them:

let M = (RAND_MAX / 6) * 6 [integer division]
r = dontCare
do { 
   r = rand()
} while (r > M)
r = r % 6 + 1 

PS, if you want to draw a 'real' number, it can be done with:

r = drawInt(1,5) // draw integer from 1 to 5 inclusive, as previously explained.
r += rand() / (RAND_MAX - 1) //the decimal part

Note that it is not a 'real' number, and the density between two numbers is 1/RAND_MAX-1

amit
  • 175,853
  • 27
  • 231
  • 333