Mail Archives: djgpp/1997/06/04/01:52:28
Paul Derbyshire (ao950 AT FreeNet DOT Carleton DOT CA) wrote:
: For values of x not a power of 2,
: random()%x
: is not as good as
: random()/(MAXINT/x)
As it stands, I disagree with this; the integer division MAXINT/x will
generally produce rounding errors which could be awkward (you will get
numbers which are too large). Performing the entire calucation cast to
double, though, can you justify this being a better generator? I remember
a thread about this a while ago, and I posted there what I thought was a
solution which would produce a fair selection of numbers. I was planning
to do some tests on it but never got around to it.
My ideal function at the moment:
int gfrandom(int range)
{
int a,b;
a=1;
while (a<range) a<<=1;
a--;
b=range;
while (b>=range) b=random()&a;
return b;
}
: Also, there's a name for a thousand million, "billion", which is somewhat
: less long and awkward :)
Fair enough, but note that in America a billion is 10^9 whereas (certainly
in the past) in Britain it is/was 10^12, which is more logical IMHO. I
tend to write `thousand million' to remove any ambiguity. I think now
`Britain' may sadly have changed to fit the American definition, which I
feel is less logical; but we shouldn't be dealing with units of 10^3
anyway...
--
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford
- Raw text -