/** * A soft-hardware RNG for as3 based on the discrepancy * between process time and the system clock. * * @author hdachev http://blog.controul.com */ package com.controul.math.rng { import flash.utils.getTimer; public class ClockDrift { public static const instance : ClockDrift = new ClockDrift (); public static function random ( bits : uint = 32 ) : uint { return instance.random ( bits ); } public function random ( bits : uint = 32 ) : uint { if ( bits > 32 ) throw new Error ( this + " @ ClockDrift::random () : Cannot return more than 32 bits: return type is uint." ); var r : uint = 0, i : uint = 0, t : uint = getTimer (), n : Number; for ( ;; ) { if ( t != ( t = getTimer () ) ) { if ( i & 1 ) r |= 1; bits --; if ( bits ) { i = 0; r <<= 1; } else break; } i ++; } return r; } } }