Seedable random number generator, actionscript

2019-07-21 11:50发布

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.

2条回答
祖国的老花朵
3楼-- · 2019-07-21 12:24

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

查看更多
登录 后发表回答