From: quickcur on
Hi, I am using the int version of rand() with MS Visual Studio. I would
like to generate random number in the range of [0, 99]. Right now I am
using

rand() * 100 / (RAN_MAX + 1)

I found that I got too much 0. 0 appears more than any other numbers.
How can I fix it?

Thanks,

qq


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
quickcur(a)yahoo.com wrote:
> Hi, I am using the int version of rand() with MS Visual Studio. I would
> like to generate random number in the range of [0, 99]. Right now I am
> using
>
> rand() * 100 / (RAN_MAX + 1)
>
> I found that I got too much 0. 0 appears more than any other numbers.
> How can I fix it?

Several things on aove code:
- RAND_MAX not RAN_MAX
- RAND_MAX+1 might overflow
- rand()*100 might overflow, too

The easiest way is to call rand() until it gives a number in your range,
obviously, but this could mean many calls.

A more sophisticated way is to first determine the largest multiple of your
range that is smaller or equal to RAND_MAX, call rand() until the value is
in that range and then simply apply the modulo operator to the result.

The reason is easy:
Assume RAND_MAX=9, i.e. rand() gives you numbers from 0 to 9. If you need
numbers in the range 0 to 3, the following mapping applies:
0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 0
5 -> 1
6 -> 2
7 -> 3
8 -> reroll
9 -> reroll


Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Carlos Moreno on
quickcur(a)yahoo.com wrote:
> Hi, I am using the int version of rand() with MS Visual Studio. I would
> like to generate random number in the range of [0, 99]. Right now I am
> using
>
> rand() * 100 / (RAN_MAX + 1)
>
> I found that I got too much 0. 0 appears more than any other numbers.
> How can I fix it?

Integer division.

Plus, try a Google search on this newsgroup for plenty of discussions
on *the* right way to do what you're trying to do (even if based on
rand(), which is a poor RNG to begin with) -- I'm talking about
discarding random numbers above the highest multiple of your number
(100 in this case).

HTH,

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thomas Maeder on
quickcur(a)yahoo.com writes:

> Hi, I am using the int version of rand() with MS Visual Studio. I would
> like to generate random number in the range of [0, 99]. Right now I am
> using
>
> rand() * 100 / (RAN_MAX + 1)
>
> I found that I got too much 0. 0 appears more than any other numbers.
> How can I fix it?

Learn how integer arithmetics works in C++.

The expression rand()*100 may cause an overflow, resulting in a number
below RAND_MAX. Dividing that number by RAND_MAX+1 will always give 0.

A naive, but better approach is

int randValue_0_100(std::rand()%100);

This will produce numbers in the range 0..99, but the lower numbers
will occur slightly more often because RAND_MAX typically isn't a
multiple of 100.


To fix for that, something like

int const upper_rand_limit(RAND_MAX/100*100);
int randValue;
do {
randValue = std::rand();
} while (randValue>=upper_rand_limit);

int randValue_0_100(randValue%100);

should work.


To be really sure about the quality of your pseudo-random numbers, you
have to carefully look at how it's documented and/or implemented, of
course.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Maxim Yegorushkin on

quickcur(a)yahoo.com wrote:
> Hi, I am using the int version of rand() with MS Visual Studio. I would
> like to generate random number in the range of [0, 99]. Right now I am
> using
>
> rand() * 100 / (RAN_MAX + 1)
>
> I found that I got too much 0. 0 appears more than any other numbers.
> How can I fix it?

(unsigned)rand() % 100;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]