Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bug in my randomizer code

16 views
Skip to first unread message

Paul

unread,
Oct 7, 2018, 12:18:38 PM10/7/18
to
I understand from a previous thread (and thanks to everyone who answered there)
that the seed of a random number generator should be kept constant
throughout the running of the program.
The below is a bugged program which tries (but fails) to generate a
random uniform number between 0 and 1.

I am aware of good solutions to the problem by others, and have been
successful myself. However, the below code has a bug and I don't understand
why. It responds by choosing the same number each time it is called.
Why did I expect it to work? Well the static key word means that the
objects get set only the first time they go through the loop and retain
their state. And isn't that exactly what we want for the generator
and the distribution?
I would expect this code to be equivalent to distribution(generator);
distribution(generator);... and that would indeed generate different
numbers. So I'd be grateful for an explanation. Making the distribution
object non-static doesn't help either.

Many thanks for your help.

double RandomGen::nextDouble() const
{
static std::default_random_engine generator(0);
static std::uniform_real_distribution<double> distribution(0.0, 1.0);
return (distribution(generator));
}

Kai-Uwe Bux

unread,
Oct 7, 2018, 12:58:56 PM10/7/18
to
On Sun, 07 Oct 2018 09:18:27 -0700, Paul wrote:

> I understand from a previous thread (and thanks to everyone who answered
> there)
> that the seed of a random number generator should be kept constant
> throughout the running of the program.
> The below is a bugged program which tries (but fails) to generate a
> random uniform number between 0 and 1.
>
> I am aware of good solutions to the problem by others, and have been
> successful myself. However, the below code has a bug and I don't
> understand why. It responds by choosing the same number each time it is
> called.

Which number ?

[Code in question:]
> double RandomGen::nextDouble() const {
> static std::default_random_engine generator(0);
> static std::uniform_real_distribution<double> distribution(0.0,
> 1.0);
> return (distribution(generator));
> }

How did you test this code? When I write a little test code like

#include <random>
#include <iostream>

double rand_double ( void ) {
static std::default_random_engine generator ( 0 );
static std::uniform_real_distribution<double> distribution ( 0.0,
1.0 );
return ( distribution( generator ) );
}


int main ( void ) {
for ( int i = 0; i < 10; ++ i ) {
std::cout << rand_double() << "\n";
}
}

I get:

0.131538
0.45865
0.218959
0.678865
0.934693
0.519416
0.0345721
0.5297
0.00769819
0.0668422

That looks pretty much random and between 0 and 1.


Best,

Kai-Uwe Bux

Ben Bacarisse

unread,
Oct 7, 2018, 2:44:35 PM10/7/18
to
Paul <peps...@gmail.com> writes:

> ... the below code has a bug and I don't understand
> why. It responds by choosing the same number each time it is called.
> Why did I expect it to work? Well the static key word means that the
> objects get set only the first time they go through the loop and retain
> their state.

You need to post a complete program that exhibits the problem you are
describing. The fragment you show looks fine, so the problem may well
be else were.

--
Ben.

Paul

unread,
Oct 7, 2018, 3:21:11 PM10/7/18
to
Yes, I think the problem is somewhere else -- the bug is in
a program that uses that code. I jumped to conclusions, sorry.

Paul
0 new messages