[random] spot the stupidity competition
Hail all,
Thanks for giving us the libraries, now only if we were all smart enough
to use them properley the world would be at peace.
As it is I've been having some problems with my own code that is wrapped
around the random library.
I'm playing around with a game I'm making and needed some randomness,
games that are predicatble are just a tad boring...
I need to generate numbers with lots of different distributions so
creating one or two won't really cut it for me, there would have to be
hundreds.
Instead I went and basically ripped the random_number_generator
described under decorators into two flavours and then created a bunch of
overloaded functions that used them.
The generators use a single real engine, currently mt19937;.
Basically, everythnig works fine, except that calling seed( n ) on the
engine doesn't effect the generators that hold a ref to it, which I
found somewhat bizzare.
So, after a few tweaks I ripped out the code and dumped it into a
minimal test case and can see that the seed( n ) method works just fine
when I pull raw numbers out of the engine, but still doesn't seem to
effect the generators I've defined.
I'm fairly sure this is my problem but I was wondering if anyone would
be good enough to take a peek and tell me why exactly this isn't working.
I did have a search on Gmane but couldn't find anything that looked
related to my problem.
I'm not sure what is the done thing on this list so I've attached
source, if you want a makefile then I can oblige but it is a fairly
small case.
The test program reads in three numbers, the seed to use, the number of
numbers to generate and the max to use when generating numbers from a
distribution.
Feeding it different seeds makes the raw number change, but not the others.
I have also included the output of two runs to show you what I mean.
TIA,
N
seed : 42
num : 10
max : 1000
seeded rng...
10 raw numbers from rng
1608637542
3421126067
4083286876
787846414
3143890026
3348747335
2571218620
2563451924
670094950
1914837113
10 random integers using rng
795
926
666
119
485
903
904
335
227
185
seed : 727364
num : 10
max : 1000
seeded rng...
10 raw numbers from rng
3240174879
3269238853
3044917573
2953574107
2463698974
2129367301
2064401652
2079989189
3644954707
1971100779
10 random integers using rng
795
926
666
119
485
903
904
335
227
185
#include "random.h"
int random_num( int max )
{
return int_gen( max );
}
int random_num( int min, int max )
{
return min + random_num( max );
}
long random_num( long max )
{
return long_gen( max );
}
long random_num( long min, long max )
{
return min + random_num( max );
}
float random_num( float max )
{
return float_gen( max );
}
float random_num( float min, float max )
{
return min + random_num( max );
}
double random_num( double max )
{
return double_gen( max );
}
double random_num( double min, double max )
{
return min + random_num( max );
}
#ifndef RANDOM_H
#define RANDOM_H
#include
In random.h, static rng_type rng; (which says, 'make this variable invisible to other object files') should be extern rng_type rng; (which says, 'declare this, but don't define it') and random.cpp should have rng_type rng; or else each translation unit (source file) will have it's own rng, meaning when you seed rng in test.cpp, random.cpp's rng is not seeded, meaning, of course, that rng() in test.cpp will give random numbers, but rng() in random.cpp will (ironicly) not give random numbers. And don't worry too much about this, linkage issues /are/ confusing.
Simon Buchan wrote:
In random.h,
static rng_type rng;
(which says, 'make this variable invisible to other object files') should be
extern rng_type rng;
(which says, 'declare this, but don't define it') and random.cpp should have
rng_type rng;
or else each translation unit (source file) will have it's own rng, meaning when you seed rng in test.cpp, random.cpp's rng is not seeded, meaning, of course, that rng() in test.cpp will give random numbers, but rng() in random.cpp will (ironicly) not give random numbers.
And don't worry too much about this, linkage issues /are/ confusing.
Gah! Thank you. I have been tainted by too much Java. Well, you win the competition then, I owe you a beer, or two. On slight tangent, do you think that the way I've done this makes sense or is there a better way of wrapping the random functionalit so I can easily get numbers in whatever range I want? n
participants (2)
-
Nigel Rantor
-
Simon Buchan