[ODE] dRandInt Crash Fix

SJS ode at pulsecode.net
Wed Jun 22 18:31:13 MST 2005


I'd have to disagree-- dRand() is quite _not_ good (if you're looking for
random, though for the reordering that it gets used for in ODE it probably
doesn't have to be very good)--

seed = (1664525*seed + 1013904223) & 0xffffffff.

just look how broken:
if seed is odd then
(odd*odd + odd) -> (odd+odd) -> even
if seed is even then
(odd*even + odd) -> (even+odd) -> odd

in other words, you get
odd,even,odd,even,odd,even....
the low bit just flips back and forth.

like the previous guy said, there are many on the web if you need.
The following seems to be reasonable and very quick (snagged and simplified
lowest order form from some web site that said it's distribution is pretty
good, and as far as I can tell, it's at least OK, but I haven't run thorough
tests on it -- you might want to add a '0xffffffff' mask if you're on a
non-32-bit platform):

static const unsigned int lcg_a = 1588635695;
static const unsigned int lcg_r = 1117695901;
static unsigned int DEFAULT_SEED = 93186752;

static unsigned int su_Seed = DEFAULT_SEED;

unsigned int dRand()
{
	unsigned int n = lcg_a*(su_Seed & 0x01) - lcg_r*(su_Seed >> 1);
	su_Seed = n ? n : DEFAULT_SEED;
	return n;
}

SJS


-----Original Message-----
From: ode-bounces at q12.org [mailto:ode-bounces at q12.org]On Behalf Of Adam
D. Moss
Sent: Wednesday, June 22, 2005 3:08 PM
To: xm_ode at xmunkki.org
Cc: ode
Subject: Re: [ODE] dRandInt Crash Fix


Jere Sanisalo wrote:
> All this talk about random generators.. :) I'd like to know the reasons
for
> not using Mersenne Twister as a random generator? The net has a readily
wide
> supply of ready routines, and as I gather, it's not too slow and it's
> integer based (which really is a boon for those developing for game
> consoles, as they may have only 32bit floating point in hw, not doubles at
> all).

ODE's PRNG is as simple+good as it needs to be for ODE.  It is, and
always has been, int-only.  The problem is specifically with dRandInt(),
not the underlying dRand() PRNG.

--adam
--
Adam D. Moss   -   adam at gimp.org
_______________________________________________
ODE mailing list
ODE at q12.org
http://q12.org/mailman/listinfo/ode



More information about the ODE mailing list