Elgoog
Elgoog

Reputation: 2275

true or false based on probability

i'm getting a bit confused with operators and there use with random generation. I guess i'm just asking does this code do what I want it to?

Generate a 'random' TRUE or FALSE depending on what probability I assigned the function.

bool randtf(int probability) {
    if ((rand() % 100) < probability)
        return true;
    else
        return false;
}

so if randtf(63) it has a 63% chance of being TRUE?

Any guidance would be much appreciated. Thanks.

Upvotes: 2

Views: 1526

Answers (4)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

Yes, to a first approximation.

No, more accurately. rand() returns a number between 0 and RAND_MAX, which in practice will always be of the form (1 << n) - 1. This isn't a multiple of 100, so you won't get a perfectly uniform distribution when you take the modulo.

You can fix that by using rejection sampling. For the sake of argument, let's assume RAND_MAX == 32767 (i.e. 16-bit). The first step is to keep generating random numbers, rejecting them until you obtain one less than 32700 (the largest multiple of 100 that's less than RAND_MAX). If you then do the modulo trick on that, you will get a uniform distribution.

Of course, this assumes a sane, statistically robust, implementation of rand(), which is quite an assumption!

Upvotes: 6

JosephH
JosephH

Reputation: 8815

Even if the random generator is perfectly random, because rand() only generates numbers from 0 to 0x7FFF, it cannot be perfectly distributed. consider the following code:

int arr[100] = { 0 };
for( int i=0; i<0x7FFF; i++ ){
arr[ i % 100 ]++;
}

for( int i=0; i<100; i++ ){
    cout << arr[i] << endl;
}
return 0;

If you run this code, it's not going to give you the same number:

328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
328
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
327
Press any key to continue . . .

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477040

It depends on what rand() returns, and in particular if the range of possible numbers is a an exact multiple of 100 (and uniformly distributed). In general, using modulo does not necessarily give you the correct statistical behaviour.

For example's sake, suppose your rand() only returned the valued 0, 1, 2, 3, 4, 5, 6; and you said rand() % 5 < p (where 0 <= p < 5). But behold: This takes the values 0 and 1 twice, but 2, 3 and 4 only once across the range of valid source numbers! So with p = 4 you don't actually get 80% probability, but only 1/7.

Upvotes: 0

MartyTPS
MartyTPS

Reputation: 530

MAX_RAND is not a multiple of 100, so your distribution would technically not be fair. You have to scale rand(), not mod it.

Upvotes: 0

Related Questions