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

random.js

parent 51150150
No related branches found
No related tags found
1 merge request!6004Move random.js for typescript friendliness
......@@ -3,22 +3,22 @@
* mean and deviation specify the desired mean and standard deviation.
* @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 sigma = 2.0 * Math.PI * (1 - Math.random());
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).
// 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.
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];
while (val < min || val > max) {
val = gaussianPair(mean, deviation)[0];
}
return Math.round(val);
};
}
/**
* 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
* @param {number} max
* @returns {number}
*/
window.jsRandom = function(min, max, count = 1) {
function jsRandom(min, max, count = 1) {
function rand() {
return Math.random() * (max - min + 1) + min;
}
......@@ -41,7 +41,7 @@ window.jsRandom = function(min, max, count = 1) {
total += rand();
}
return Math.floor(total/count);
};
}
/**
* Chooses multiple random elements of an array.
......@@ -49,7 +49,7 @@ window.jsRandom = function(min, max, count = 1) {
* @param {number} count
* @returns {number[]}
*/
window.jsRandomMany = function(arr, count) {
function jsRandomMany(arr, count) {
let result = [];
let _tmp = arr.slice();
for (let i = 0; i < count; i++) {
......@@ -57,7 +57,7 @@ window.jsRandomMany = function(arr, count) {
result.push(_tmp.splice(index, 1)[0]);
}
return result;
};
}
/**
* Accepts both an array and a list, returns undefined if nothing is passed.
......@@ -65,11 +65,11 @@ window.jsRandomMany = function(arr, count) {
* @param {...(number|string|number[]|string[])} otherChoices
* @returns {number|string|number[]|string[]|undefined}
*/
window.jsEither = function(choices, ...otherChoices) {
function jsEither(choices, ...otherChoices) {
if (otherChoices.length === 0 && Array.isArray(choices)) {
return choices[Math.floor(Math.random() * choices.length)];
}
const allChoices = otherChoices;
allChoices.push(choices);
return allChoices[Math.floor(Math.random() * allChoices.length)];
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment