File indexing completed on 2024-05-12 05:27:53
0001 /* 0002 * Rnd (JS) by Grant Skinner. June 18, 2011 0003 * based on Rnd (AS3) by Grant Skinner. Dec 5, 2008 0004 * Visit http://github.com/gskinner for documentation, updates and examples. 0005 * 0006 * 0007 * Copyright (c) 2011 Grant Skinner 0008 * 0009 * Permission is hereby granted, free of charge, to any person 0010 * obtaining a copy of this software and associated documentation 0011 * files (the "Software"), to deal in the Software without 0012 * restriction, including without limitation the rights to use, 0013 * copy, modify, merge, publish, distribute, sublicense, and/or sell 0014 * copies of the Software, and to permit persons to whom the 0015 * Software is furnished to do so, subject to the following 0016 * conditions: 0017 * 0018 * The above copyright notice and this permission notice shall be 0019 * included in all copies or substantial portions of the Software. 0020 * 0021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 0022 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 0023 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 0024 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 0025 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 0026 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 0027 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 0028 * OTHER DEALINGS IN THE SOFTWARE. 0029 */ 0030 0031 // Rnd is not a constructor. 0032 function rnd(min,max) { 0033 if (isNaN(max)) { max = min; min=0; } 0034 if (isNaN(max)) { max = 1; } 0035 return rnd.random()*(max-min)+min; 0036 }; 0037 0038 // public static methods: 0039 // random(); // returns a number between 0-1 exclusive. 0040 rnd.random = function() { 0041 return Math.random(); 0042 }; 0043 0044 // float(50); // returns a number between 0-50 exclusive 0045 // float(20,50); // returns a number between 20-50 exclusive 0046 rnd.float = rnd; 0047 0048 // boolean(); // returns true or false (50% chance of true) 0049 // boolean(0.8); // returns true or false (80% chance of true) 0050 rnd.boolean = function(chance) { 0051 if (isNaN(chance)) { chance = 0.5; } 0052 return (rnd.random() < chance); 0053 } 0054 0055 // sign(); // returns 1 or -1 (50% chance of 1) 0056 // sign(0.8); // returns 1 or -1 (80% chance of 1) 0057 rnd.sign = function(chance) { 0058 if (isNaN(chance)) { chance = 0.5; } 0059 return (rnd.random() < chance) ? 1 : -1; 0060 } 0061 0062 // bit(); // returns 1 or 0 (50% chance of 1) 0063 // bit(0.8); // returns 1 or 0 (80% chance of 1) 0064 rnd.bit = function(chance) { 0065 if (isNaN(chance)) { chance = 0.5; } 0066 return (rnd.random() < chance) ? 1 : 0; 0067 } 0068 0069 // integer(50); // returns an integer between 0-49 inclusive 0070 // integer(20,50); // returns an integer between 20-49 inclusive 0071 rnd.integer = function(min,max) { 0072 if (isNaN(max)) { max = min; min=0; } 0073 // Need to use floor instead of bit shift to work properly with negative values: 0074 return Math.floor(rnd.float(min,max)); 0075 } 0076 0077 // shuffle(arr); // shuffles the items in the specified array. Modifies the original array. 0078 // arr2 = Rnd.shuffle(arr1.slice()); // to get a new shuffled array w/o modifying original. 0079 // no allocations or array resizing. 0080 rnd.shuffle = function(array) { 0081 var l = array.length; 0082 for (var i=0; i<l; i++) { 0083 var j = l*rnd.random()|0; 0084 if (j==i) { continue; } 0085 var item = array[j]; 0086 array[j] = array[i]; 0087 array[i] = item; 0088 } 0089 return array; 0090 } 0091 0092 // item([1,3,5]); // returns a random item from the array. Does not modify the original array. 0093 rnd.item = function(array) { 0094 return array[array.length*rnd.random()|0]; 0095 }