Make Random object lowercase

This commit is contained in:
Christoph Diehl 2017-04-07 10:10:59 +02:00
parent 7b5855e18f
commit c42604e984

View file

@ -1,4 +1,4 @@
var Random = { var random = {
twister: null, twister: null,
init: function (seed) { init: function (seed) {
@ -26,18 +26,18 @@ var Random = {
// Returns an integer in [start, limit]. Uniform distribution. // Returns an integer in [start, limit]. Uniform distribution.
if (isNaN(start) || isNaN(limit)) { if (isNaN(start) || isNaN(limit)) {
Utils.traceback(); Utils.traceback();
throw new TypeError("Random.range() received a non number type: '" + start + "', '" + limit + "')"); throw new TypeError("random.range() received a non number type: '" + start + "', '" + limit + "')");
} }
return Random.number(limit - start + 1) + start; return random.number(limit - start + 1) + start;
}, },
ludOneTo: function(limit) { ludOneTo: function(limit) {
// Returns a float in [1, limit]. The logarithm has uniform distribution. // Returns a float in [1, limit]. The logarithm has uniform distribution.
return Math.exp(Random.float() * Math.log(limit)); return Math.exp(random.float() * Math.log(limit));
}, },
index: function (list) { index: function (list) {
if (!(list instanceof Array || (typeof list != "string" && "length" in list))) { if (!(list instanceof Array || (typeof list != "string" && "length" in list))) {
Utils.traceback(); Utils.traceback();
throw new TypeError("Random.index() received a non array type: '" + list + "'"); throw new TypeError("random.index() received a non array type: '" + list + "'");
} }
return list[this.number(list.length)]; return list[this.number(list.length)];
}, },
@ -66,14 +66,14 @@ var Random = {
} }
if (isNaN(limit)) { if (isNaN(limit)) {
Utils.traceback(); Utils.traceback();
throw new TypeError("Random.chance() received a non number type: '" + limit + "'"); throw new TypeError("random.chance() received a non number type: '" + limit + "'");
} }
return this.number(limit) == 1; return this.number(limit) == 1;
}, },
choose: function (list, flat) { choose: function (list, flat) {
if (!(list instanceof Array)) { if (!(list instanceof Array)) {
Utils.traceback(); Utils.traceback();
throw new TypeError("Random.choose() received a non-array type: '" + list + "'"); throw new TypeError("random.choose() received a non-array type: '" + list + "'");
} }
var total = 0; var total = 0;
for (var i = 0; i < list.length; i++) { for (var i = 0; i < list.length; i++) {
@ -96,7 +96,7 @@ var Random = {
return this.pick([list[0][1]]); return this.pick([list[0][1]]);
}, },
weighted: function (wa) { weighted: function (wa) {
// More memory-hungry but hopefully faster than Random.choose$flat // More memory-hungry but hopefully faster than random.choose$flat
var a = []; var a = [];
for (var i = 0; i < wa.length; ++i) { for (var i = 0; i < wa.length; ++i) {
for (var j = 0; j < wa[i].w; ++j) { for (var j = 0; j < wa[i].w; ++j) {
@ -106,13 +106,13 @@ var Random = {
return a; return a;
}, },
use: function (obj) { use: function (obj) {
return Random.bool() ? obj : ""; return random.bool() ? obj : "";
}, },
shuffle: function (arr) { shuffle: function (arr) {
var len = arr.length; var len = arr.length;
var i = len; var i = len;
while (i--) { while (i--) {
var p = Random.number(i + 1); var p = random.number(i + 1);
var t = arr[i]; var t = arr[i];
arr[i] = arr[p]; arr[i] = arr[p];
arr[p] = t; arr[p] = t;
@ -120,13 +120,13 @@ var Random = {
}, },
shuffled: function (arr) { shuffled: function (arr) {
var newArray = arr.slice(); var newArray = arr.slice();
Random.shuffle(newArray); random.shuffle(newArray);
return newArray; return newArray;
}, },
some: function (list, limit) { some: function (list, limit) {
if (!(list instanceof Array)) { if (!(list instanceof Array)) {
Utils.traceback(); Utils.traceback();
throw new TypeError("Random.some() received a non-array type: '" + list + "'"); throw new TypeError("random.some() received a non-array type: '" + list + "'");
} }
if (typeof limit == 'number') { if (typeof limit == 'number') {
limit = this.range(0, list.length); limit = this.range(0, list.length);