Question

I was wondering if there is a smart way to find out

There is a 1/4 chance something happens.

I know we can do this with rand() % 4 and checking if it is equal to 0, but is there a way without using rand()? In c++, thanks.

No correct solution

OTHER TIPS

If you mean you want to avoid the inherent crappiness of many rand() implementations, you should probably look into the Boost Random library, which has several high-quality pRNGs (pseudo-random number generators), and many ways to control the output. This library is also present in slightly modified form in std::tr1.

Never ever use % for truncating a PRNG value into a range. Most PRNGs have relatively non-random lower order bits.

For your case, use a division (RAND_MAX / n) like BCS suggests.

random number generator

rand() < RAND_MAX/n;

pick a better rand() than C's if you don't like C's standard rand().

You could write your own rand. ( dont do it ).
You could grab the tickcount. ( dont do it too often ).
You could just count, and every fourth call return true.

You should probably just call rand().

I don't know much C++, so I might be wrong. But it seems rand() return a value between 0 and RAND_MAX-1. So maybe you could do something like this:

double odds = .25;

if(rand() <= RAND_MAX * odds) {
    // there should be .25 chance of entering this condition
}

PS: Maybe this requires some casting.

Why not use rand()? If you are concerned about "true" randomness vs. pseudo randomness, you can try using physical sources of random bits. Much more complicated, and usually unnecessary.

You could use another type of RNG such as the Mersenne twister which has better overall entropy. I also hear good thing about Multuply with Carry RNGs.

4 is a special case. You can assume that your PRNG has got 50% chances of outputting an even number, which is the case - I think - for the LCG of the libc (rand). The probability of outputting an even number twice is therefore 25%.

Therefore...

bool rand_afourth(void)
{
    return !!((rand() & 1) & (rand() & 1));
}

And now for the pedantic...

What you want to do is to have an uniform random generated, but restricted to a certain range, in this case an entropy of 4. If your PRNG has, say, an entropy of 32-bit, you cannot be certain that computing the output mod 4 will work as expected. This require a bit more work.

Fortunately, this work has already been implemented in the boost library.

boost::uniform_int<> aFourth(1,4)

And you would for example say "ok" everytime you get 1 (or 2, 3, 4, as you fancy).

But you may not want to use the boost library. Then, simply look at the code of uniform_int and reproduce the behaviour. Talents imitate, geniuses steal. ;)

Umm... write your own rand()? You will need some kind of random function!

Try:

static int r = 0;
: : :
if ((r = (r+1)%4) == 0) {
    // do something.
}

Then you'll find it gives you a perfect 25% probability of something happening (assuming you execute the if-statement a multiple of four times.

</humor>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top