From: Pete Becker on
kevin.hall(a)motioneng.com wrote:

>
> Ok, fine. But again, the C library implementation of rand() is
> notorious for this behavior

Which C library is "the C library"?


-- and that is what people in this thread
> are recommending. Perhaps not all rand() implementations are bad --
> but some certainly are.

Maybe. And maybe some implementations of the mersenne twister are bad.

>
> Anyway, it seems that many people are fighting to defend their practice
> of using modulo.

Maybe. I'm defending thinking instead of basing engineering decisions on
rumors.

>
>>No, it addresses a different, real, problem.
>
>
> I should have been more clear. I understand that some slots in general
> will get more hits than others. I was trying to address the problem
> from a different perspective. In my example, I ended up with 8 valid
> slots (just like Ulrich's example) and that is what I meant by "This is
> the same as above".
>
> Does this make sense?
>

No, because it's not the same as above. It's a different problem.
Properly reducing the number of values eliminates the imbalance that
results from a mismatched number of slots. That's a completely different
problem from lack of randomness in low-order bits, assuming it's a
problem in the first place.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

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

From: Alberto Ganesh Barbati on
quickcur(a)yahoo.com ha scritto:
> 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?

I've seen a lot of discussion about rand(), modulo and division in this
thread... I'm not going to delve into that, but rather I suggest you
throw away rand() as soon as possible and consider using Boost.Random
(http://boost.org/libs/random/) as a replacement. It's a solid
peer-reviewed random generator library, which means all gory details
have probably been settled, so that you don't need to bother about them.

Boost.Random has been considered as a reference implementation for the
upcoming TR1, so I bet it's going to become quite popular (at least I
hope so).

Just my opinion,

Ganesh

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

From: Francis Glassborow on
In article <1139504596.550672.184530(a)z14g2000cwz.googlegroups.com>,
kevin.hall(a)motioneng.com writes
>Anyway, it seems that many people are fighting to defend their practice
>of using modulo. Division uses higher order bits which should be the
>most random in all good PRNGs. Modulo uses the lowest order bits which
>is known to NOT be very random in some cases. So which is better
>practice for general programming?

You must qualify your assertion to modulo a power of 2. Those are the
only ones where the high order bits have no contribution.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


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

From: Francis Glassborow on
In article <mvednW2NC-NxiHHeRVn-pg(a)giganews.com>, Pete Becker
<petebecker(a)acm.org> writes
>Carlos Moreno wrote:
>
>>
>> rand() tends to be one of those implementations in which the
>> above tends to be true.
>>
>
>More accurately, it is frequently said that some implementations of
>rand() have that problem.

I think some of the early ones did (and a common one used in Basic was
terrible) but I think that most modern implementations have fixed that
(yes Pete, I am on your side :-)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


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

From: Francis Glassborow on
In article <1139503966.627601.168480(a)o13g2000cwo.googlegroups.com>,
kevin.hall(a)motioneng.com writes
>What denotes a good random number generator? No PSEUDO-random number
>generator is good compared to a TRUE random number generator.

Sorry but a PRNG and a RNG are aimed at entirely different things. And
define what you mean by a true RNG. There are some pretty lousy RNG's
around (ever seen the care that has to be taken when a really random
choice must be maid with all elements being equally likely?

Many engineering problems require a sequence of 'mathematically random
values' and that sequence must be repeatable so that tests can be
repeated. Other cases such picking lottery numbers need a real method
for picking numbers at random (an ever noticed how much testing goes
into the mechanisms for lotteries to avoid bias?)

TR1 (IIRC) provides the user with a range of PRNG tools. And on a modern
fast desktop with a raw keyboard read I can provide a pretty good RNG
for what ever range of values you want.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


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