Random seed – actionscript

ActionScript, Code snippets

If you want a fast random seed, you could use this very simple class.

This blogpost is mostly a useful copy-paste location for myself. I got this solution from http://www.calypso88.com, but the blog seems down for a while now.


package nl.stroep.utils
{
import flash.utils.getTimer;

public class Random
{
private const MAX_RATIO:Number = 1 / uint.MAX_VALUE;
private var r:uint;

public function Random(seed:uint = 0)
{
r = seed || getTimer();
}

// returns a random Number from 0 – 1
public function getNext():Number
{
r ^= (r << 21); r ^= (r >>> 35);
r ^= (r << 4); return (r * MAX_RATIO); } } } [/as] You could use it like this: [as] var random:Random = new Random(777); // fixed seed trace( random.getNext() ); // 0.7393220608912693 trace( random.getNext() ); // 0.017692924714110075 trace( random.getNext() ); // 0.08819738521431512 trace( random.getNext() ); // 0.5297092030592517 [/as] Every time you run it (with a fixed seed), you'll get the same 'random' numbers.

Say something interesting

Please link to code from an external resource, like gist.github.com.