Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 146 lines (145 sloc) 4.797 kB
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
1 package skyboy.utils {
2 import flash.utils.getTimer;
3 public class Random {
4 private static const r:Random = new Random((new Date().getTime() & 0xFFFFFFFF) ^ getTimer());
5 /**
6 * randomNumber
7 * @return Number: A number that is less than or equal to zero and less than one
8 */
9 public static function randomNumber():Number {
10 return r.extractNumber();
11 }
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
12 private const MT:Vector.<uint> = new Vector.<uint>(624, true);
bc4d36a Added ability to get and set the seed in Random
skyboy authored
13 private var index:int, _seed:int;
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
14 private const yA1:uint = 2636928640, yA2:uint = 4022730752;
15 private const a:uint = 2147483648, b:uint = 2147483647, c:uint = 2567483615, d:uint = 624, e:uint = 397;
16 private static const I_MAX:uint = int.MAX_VALUE;
17 private static const U_MAX:uint = uint.MAX_VALUE;
18 private static const iDIV:Number = I_MAX + Number.MIN_VALUE + 1;
19 private static const uDIV:Number = U_MAX + Number.MIN_VALUE;
bc4d36a Added ability to get and set the seed in Random
skyboy authored
20 /**
21 * Get the seed currently in use
22 */
23 public function get seed():int {
24 return _seed;
25 }
26 /**
27 * Set the seed currently in use (resets state)
28 */
29 public function set seed(a:int):void {
30 if (a != _seed) {
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
31 _seed = a;
32 var i:int, b:int = a;
33 index = i;
34 MT[i] = a;
35 while (++i < 624) {
36 b = (MT[i] = (1812433253 * Number(b ^ (b >>> 30)) + i) & 0xFFFFFFFF);
37 }
bc4d36a Added ability to get and set the seed in Random
skyboy authored
38 }
39 }
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
40 /**
41 * @param int: seed The seed number to use, using the same seed will get you the same results each time
42 */
43 public function Random(seed:int):void {
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
44 _seed = seed - 1;
45 this.seed = seed;
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
46 }
47 /**
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
48 * Returns a number that is greater than or equal to zero and less than one
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
49 * @return Number: A number that is greater than or equal to zero and less than one
50 */
51 public function extractNumber():Number {
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
52 var i:int = index, MT:Vector.<uint> = this.MT, y:uint = MT[i];
53 index = (i + 1) % 624;
54 var u:uint = (y & a) | (MT[index] & b);
55 MT[i] = (MT[(i + e) % d] ^ (u >>> 1)) ^ (c & (-(u & 1)));
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
56 y ^= y >>> 11;
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
57 y ^= (y << 7) & yA1;
58 y ^= (y << 15) & yA2;
59 y ^= y >>> 18;
60 return y / uDIV;
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
61 }
62 /**
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
63 * Returns a number that is greater than negative one and less than one
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
64 * @return Number: A number that is greater than negative one and less than one
65 */
66 public function extractNumber2():Number {
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
67 var i:int = index, MT:Vector.<uint> = this.MT, y:uint = MT[i];
68 index = (i + 1) % 624;
69 var u:uint = (y & a) | (MT[index] & b);
70 MT[i] = (MT[(i + e) % d] ^ (u >>> 1)) ^ (c & (-(u & 1)));
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
71 y ^= y >>> 11;
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
72 y ^= (y << 7) & yA1;
73 y ^= (y << 15) & yA2;
74 return (y ^ (y >>> 18)) / iDIV;
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
75 }
76 /**
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
77 * Returns a number that is greater than negative 0.5 and less than 0.5
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
78 * @return Number: A number that is greater than negative 0.5 and less than 0.5
79 */
80 public function extractNumber3():Number {
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
81 var i:int = index, MT:Vector.<uint> = this.MT, y:uint = MT[i];
82 index = (i + 1) % 624;
83 var u:uint = (y & a) | (MT[index] & b);
84 MT[i] = (MT[(i + e) % d] ^ (u >>> 1)) ^ (c & (-(u & 1)));
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
85 y ^= y >>> 11;
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
86 y ^= (y << 7) & yA1;
87 y ^= (y << 15) & yA2;
88 return int(y ^ (y >>> 18)) / uDIV;
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
89 }
90 /**
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
91 * Returns an unsigned integer between 0 and uint.MAX_VALUE
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
92 * @return uint: An unsigned integer between 0 and uint.MAX_VALUE
93 */
94 public function extractUint():uint {
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
95 var i:int = index, MT:Vector.<uint> = this.MT, y:uint = MT[i];
96 index = (i + 1) % 624;
97 var u:uint = (y & a) | (MT[index] & b);
98 MT[i] = (MT[(i + e) % d] ^ (u >>> 1)) ^ (c & (-(u & 1)));
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
99 y ^= y >>> 11;
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
100 y ^= (y << 7) & yA1;
101 y ^= (y << 15) & yA2;
28bc2fb Fixed a posible glitch in Random
unknown authored
102 return (y ^ (y >>> 18));
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
103 }
104 /**
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
105 * Returns an unsigned integer between min and max
106 * @return uint: An unsigned integer between min and max
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
107 */
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
108 public function extractBoundedUint(min:uint, max:uint):uint {
109 var i:int = index, MT:Vector.<uint> = this.MT, y:uint = MT[i];
110 index = (i + 1) % 624;
111 var u:uint = (y & a) | (MT[index] & b);
112 MT[i] = (MT[(i + e) % d] ^ (u >>> 1)) ^ (c & (-(u & 1)));
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
113 y ^= y >>> 11;
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
114 y ^= (y << 7) & yA1;
115 y ^= (y << 15) & yA2;
116 y ^= y >>> 18;
117 if (max < min) {
118 return (y % (min + 1 - max)) + max;
119 }
120 return (y % (max + 1 - min)) + min;
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
121 }
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
122 /**
123 * extractExperimentalUint
124 * @return int: An unsigned integer between 0 and uint.MAX_VALUE
125 * /
126 public function extractExperimentalUint():uint {
127 var i:int = index, MT:Vector.<uint> = this.MT, y:uint = MT[i];
128 index = (i + 1) % 624;
129 var u:uint = (y & a) | (MT[index] & b);
130 MT[i] = (MT[(i + e) % d] ^ (u >>> 1)) ^ (c & (-(u & 1)));
131 y ^= y >>> 11;
132 y ^= (y << 7) & yA1;
133 y ^= (y << 15) & yA2;
134 return (y ^ (y >>> 18));
135 }//*/
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
136 private function generateNumbers():void {
1dac03a @skyboy Decreased utils.Random execution time by approximately 70%.
authored
137 var i:int, y:int, MT:Vector.<uint> = this.MT;
138 while (i < 624) {
139 y = (MT[i] & a) | (MT[(i+1) % d] & b);
140 MT[i] = (MT[(i + e) % d] ^ (y >>> 1)) ^ (c & (-(y & 1)));
141 ++i;
cf8f93b Add class Random, which implements Mersenne Twister 19937 in AS3
unknown authored
142 }
143 }
144 }
145 }
Something went wrong with that request. Please try again.