'JS generate random boolean
Simple question, but I'm interested in the nuances here.
I'm generating random booleans using the following method I came up with myself:
const rand = Boolean(Math.round(Math.random()));
Whenever random() shows up, it seems there's always a pitfall - it's not truly random, it's compromised by something or other, etc. So, I'd like to know:
a) Is the above the best-practice way to do it?
b) Am I overthinking things?
c) Am I underthinking things?
d) Is there a better/faster/elegant-er way I don't know of?
(Also somewhat interested if B and C are mutually exclusive.)
Update
If it makes a difference, I'm using this for movement of an AI character.
Solution 1:[1]
If your project has lodash then you can:
_.sample([true, false])
Alternatively you can use your own sample function (source):
const sample = arr => arr[Math.floor(Math.random() * arr.length)];
Solution 2:[2]
For a more cryptographically secure value, you can use crypto.getRandomValues in modern browsers.
Sample:
var randomBool = (function() {
var a = new Uint8Array(1);
return function() {
crypto.getRandomValues(a);
return a[0] > 127;
};
})();
var trues = 0;
var falses = 0;
for (var i = 0; i < 255; i++) {
if (randomBool()) {
trues++;
}
else {
falses++;
}
}
document.body.innerText = 'true: ' + trues + ', false: ' + falses;
Note that the crypto object is a DOM API, so it's not available in Node, but there is a similar API for Node.
Solution 3:[3]
!Math.round(Math.random());
Solution 4:[4]
Impressed a lot by Kelvin's answer I would like to suggest a fairly similar but slightly enhanced solution.
var randomBoolean = Math.random() < 0.5;
This solution is a bit more obvious to read, because the number on the right-hand side of < tells you the probability of getting true rather than of getting false, which is more natural to comprehend. Also < is one symbol shorter than >=;
Solution 5:[5]
Potentialy faster solutions...
Bitwise operator approach i just thought of Math.random() + .5 >> 0 or ~~(Math.random() + .5). Here is a performance test to judge for yourself.
let randomBoolean = Math.random() + .5 >> 0; //chance of true
const randomBoolean = chance => Math.random() + chance >> 0; //chance of true
The bitwise operators is in this case essentially just the same as using Math.trunc() or Math.floor(), therefore this is also posible Math.trunc(Math.random() + .5).
let randomBoolean = Math.trunc(Math.random() + .5);
const randomBoolean = chance => Math.trunc(Math.random() + chance);
Other more common solutions
The more common way to get random boolean is probably a comparative approach like Math.random() >= .5 from Kelvin's answer or Math.random() < .5; from Arthur Khazbs's answer, they actualy output true & false, and not 1 & 0.
let randomBoolean = Math.random() >= .5; //chance of false
const randomBoolean = chance => Math.random() >= chance; //chance of false
let randomBoolean = Math.random() < .5; //chance of true
const randomBoolean = chance => Math.random() < chance; //chance of true
The only reason to use the Math.round(Math.random()) approach is simplicity and laziness.
Solution 6:[6]
let try most easy solutions
let status=Math.round(Math.random())
console.log(status)
if(status==1)
{
status=true
}else{
status=false
}
console.log(status)
Solution 7:[7]
How about this one?
return Math.round((Math.random() * 1) + 0) === 0;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Nato Boram |
| Solution 4 | Arthur Khazbs |
| Solution 5 | |
| Solution 6 | Rishu Singh |
| Solution 7 | Alex |
