Seedable random number generator, actionscript

2019-07-21 12:13发布

问题:

I am looking for a random number generator in actionscript 3.0 that i can seed. The Math.random() does not have this functionality.

Thanks.

回答1:

Here you go http://gskinner.com/blog/archives/2008/01/source_code_see.html



回答2:

I mostly use this seeded random function, which is very fast.

var seed:int = 777;

const MAX_RATIO:Number = 1 / int.MAX_VALUE;
const MIN_MAX_RATIO:Number = -MAX_RATIO;

function random():Number
{
   seed ^= (seed << 21);
   seed ^= (seed >>> 35);
   seed ^= (seed << 4);
   if (seed > 0) return seed * MAX_RATIO;
   return seed * MIN_MAX_RATIO;
}

sources:
http://blog.stroep.nl/2012/07/random-seed-actionscript/
http://en.wikipedia.org/wiki/Linear_congruential_generator