1 /** 2 * The Render Engine 3 * Math2 Class 4 * 5 * @fileoverview A math static class which provides a method for generating 6 * pseudo random numbers. 7 * 8 * @author: Brett Fattori (brettf@renderengine.com) 9 * @author: $Author: bfattori $ 10 * @version: $Revision: 1555 $ 11 * 12 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a copy 15 * of this software and associated documentation files (the "Software"), to deal 16 * in the Software without restriction, including without limitation the rights 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 * copies of the Software, and to permit persons to whom the Software is 19 * furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be included in 22 * all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 * THE SOFTWARE. 31 * 32 */ 33 34 /** 35 * @class A static class which provides methods for generating random integers 36 * and floats between 0 and 1. The class also provides a way to seed the 37 * random number generator for repeatable results. 38 * 39 * @static 40 */ 41 R.lang.Math2 = /** @scope R.lang.Math2.prototype */{ 42 43 state:1, 44 m:0x100000000, // 2**32; 45 a:1103515245, 46 c:12345, 47 48 /** 49 * Largest integer (4294967295) 50 * @type {Number} 51 * @memberOf R.lang.Math2 52 */ 53 MAX_INT:0xFFFFFFFF, // 64-bits 54 55 /** 56 * Seed the random number generator with a known number. This 57 * ensures that random numbers occur in a known sequence. 58 * 59 * @param seed {Number} An integer to seed the number generator with 60 * @memberOf R.lang.Math2 61 */ 62 seed:function (seed) { 63 // LCG using GCC's constants 64 R.lang.Math2.state = seed ? seed : Math.floor(Math.random() * (R.lang.Math2.m - 1)); 65 }, 66 67 /** 68 * Returns a random integer between 0 and 4,294,967,296. 69 * @return {Number} An integer between 0 and 2^32 70 * @memberOf R.lang.Math2 71 */ 72 randomInt:function () { 73 R.lang.Math2.state = (R.lang.Math2.a * R.lang.Math2.state + R.lang.Math2.c) % R.lang.Math2.m; 74 return R.lang.Math2.state; 75 }, 76 77 /** 78 * Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive) 79 * @return {Number} A number between 0 and 1 80 * @memberOf R.lang.Math2 81 */ 82 random:function () { 83 // returns in range [0,1] 84 return R.lang.Math2.randomInt() / (R.lang.Math2.m - 1); 85 }, 86 87 /** 88 * Return a random value within the <tt>low</tt> to <tt>height</tt> range, 89 * optionally as an integer value only. 90 * 91 * @param low {Number} The low part of the range 92 * @param high {Number} The high part of the range 93 * @param [whole] {Boolean} Return whole values only 94 * @return {Number} 95 * @memberOf R.lang.Math2 96 */ 97 randomRange:function (low, high, whole) { 98 var v = low + (R.lang.Math2.random() * high); 99 return (whole ? Math.floor(v) : v); 100 }, 101 102 /** 103 * Parse a binary string into a number. 104 * 105 * @param bin {String} Binary string to parse 106 * @return {Number} 107 * @memberOf R.lang.Math2 108 */ 109 parseBin:function (bin) { 110 if (!isNaN(bin)) { 111 return R.global.parseInt(bin, 2); 112 } 113 }, 114 115 /** 116 * Converts a number to a hexidecimal string, prefixed by "0x". 117 * 118 * @param num {Number} The number to convert 119 * @return {String} 120 * @memberOf R.lang.Math2 121 */ 122 toHex:function (num) { 123 if (!isNaN(num)) { 124 return ("0x" + num.toString(16)); 125 } 126 }, 127 128 /** 129 * Converts a number to a binary string. 130 * 131 * @param num {Number} The number to convert 132 * @return {String} 133 * @memberOf R.lang.Math2 134 */ 135 toBinary:function (num) { 136 if (!isNaN(num)) { 137 return num.toString(2); 138 } 139 } 140 }; 141 142 // Initially seed the random number generator with a pseudo-random number 143 R.lang.Math2.seed(); 144