Skip to content
Snippets Groups Projects
Commit 721a6d50 authored by Skriv's avatar Skriv
Browse files

random.js

parent 51150150
Branches
Tags
1 merge request!6004Move random.js for typescript friendliness
...@@ -3,22 +3,22 @@ ...@@ -3,22 +3,22 @@
* mean and deviation specify the desired mean and standard deviation. * mean and deviation specify the desired mean and standard deviation.
* @returns {number[]} * @returns {number[]}
*/ */
window.gaussianPair = function(mean = 0, deviation = 1) { function gaussianPair(mean = 0, deviation = 1) {
const r = Math.sqrt(-2.0 * Math.log(1 - Math.random())); const r = Math.sqrt(-2.0 * Math.log(1 - Math.random()));
const sigma = 2.0 * Math.PI * (1 - Math.random()); const sigma = 2.0 * Math.PI * (1 - Math.random());
return [r * Math.cos(sigma), r * Math.sin(sigma)].map(val => val * deviation + mean); return [r * Math.cos(sigma), r * Math.sin(sigma)].map(val => val * deviation + mean);
}; }
// Generate a random integer with a normal distribution between min and max (both inclusive). // Generate a random integer with a normal distribution between min and max (both inclusive).
// Default parameters result in truncating the standard normal distribution between -3 and +3. // Default parameters result in truncating the standard normal distribution between -3 and +3.
// Not specifying min/max results in rerolling val approximately 0.3% of the time. // Not specifying min/max results in rerolling val approximately 0.3% of the time.
window.normalRandInt = function(mean = 0, deviation = 1, min = mean - 3 * deviation, max = mean + 3 * deviation) { function normalRandInt(mean = 0, deviation = 1, min = mean - 3 * deviation, max = mean + 3 * deviation) {
let val = gaussianPair(mean, deviation)[0]; let val = gaussianPair(mean, deviation)[0];
while (val < min || val > max) { while (val < min || val > max) {
val = gaussianPair(mean, deviation)[0]; val = gaussianPair(mean, deviation)[0];
} }
return Math.round(val); return Math.round(val);
}; }
/** /**
* Returns a random integer between min and max (both inclusive). * Returns a random integer between min and max (both inclusive).
...@@ -27,7 +27,7 @@ window.normalRandInt = function(mean = 0, deviation = 1, min = mean - 3 * deviat ...@@ -27,7 +27,7 @@ window.normalRandInt = function(mean = 0, deviation = 1, min = mean - 3 * deviat
* @param {number} max * @param {number} max
* @returns {number} * @returns {number}
*/ */
window.jsRandom = function(min, max, count = 1) { function jsRandom(min, max, count = 1) {
function rand() { function rand() {
return Math.random() * (max - min + 1) + min; return Math.random() * (max - min + 1) + min;
} }
...@@ -41,7 +41,7 @@ window.jsRandom = function(min, max, count = 1) { ...@@ -41,7 +41,7 @@ window.jsRandom = function(min, max, count = 1) {
total += rand(); total += rand();
} }
return Math.floor(total/count); return Math.floor(total/count);
}; }
/** /**
* Chooses multiple random elements of an array. * Chooses multiple random elements of an array.
...@@ -49,7 +49,7 @@ window.jsRandom = function(min, max, count = 1) { ...@@ -49,7 +49,7 @@ window.jsRandom = function(min, max, count = 1) {
* @param {number} count * @param {number} count
* @returns {number[]} * @returns {number[]}
*/ */
window.jsRandomMany = function(arr, count) { function jsRandomMany(arr, count) {
let result = []; let result = [];
let _tmp = arr.slice(); let _tmp = arr.slice();
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
...@@ -57,7 +57,7 @@ window.jsRandomMany = function(arr, count) { ...@@ -57,7 +57,7 @@ window.jsRandomMany = function(arr, count) {
result.push(_tmp.splice(index, 1)[0]); result.push(_tmp.splice(index, 1)[0]);
} }
return result; return result;
}; }
/** /**
* Accepts both an array and a list, returns undefined if nothing is passed. * Accepts both an array and a list, returns undefined if nothing is passed.
...@@ -65,11 +65,11 @@ window.jsRandomMany = function(arr, count) { ...@@ -65,11 +65,11 @@ window.jsRandomMany = function(arr, count) {
* @param {...(number|string|number[]|string[])} otherChoices * @param {...(number|string|number[]|string[])} otherChoices
* @returns {number|string|number[]|string[]|undefined} * @returns {number|string|number[]|string[]|undefined}
*/ */
window.jsEither = function(choices, ...otherChoices) { function jsEither(choices, ...otherChoices) {
if (otherChoices.length === 0 && Array.isArray(choices)) { if (otherChoices.length === 0 && Array.isArray(choices)) {
return choices[Math.floor(Math.random() * choices.length)]; return choices[Math.floor(Math.random() * choices.length)];
} }
const allChoices = otherChoices; const allChoices = otherChoices;
allChoices.push(choices); allChoices.push(choices);
return allChoices[Math.floor(Math.random() * allChoices.length)]; return allChoices[Math.floor(Math.random() * allChoices.length)];
}; }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment