Reputation: 57287
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.
Upvotes: 274
Views: 212343
Reputation: 112
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);
The more readable and 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.
Upvotes: 3
Reputation: 9043
Sometimes I just need a "random" boolean for a teaching example or something. Depending on your use-case, this might be random enough.
function flipCoin() {
return Boolean(Date.now() % 2);
}
console.log( flipCoin() );
Upvotes: 2
Reputation: 19
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)
Upvotes: 1
Reputation: 7618
You can compare Math.random()
to 0.5
directly, as the range of Math.random()
is [0, 1)
(this means 'in the range 0 to 1 including 0, but not 1'). You can divide the range into [0, 0.5)
and [0.5, 1)
.
var random_boolean = Math.random() < 0.5;
// Example
console.log(Math.random() < 0.1); //10% probability of getting true
console.log(Math.random() < 0.4); //40% probability of getting true
console.log(Math.random() < 0.5); //50% probability of getting true
console.log(Math.random() < 0.8); //80% probability of getting true
console.log(Math.random() < 0.9); //90% probability of getting true
Upvotes: 680
Reputation:
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)];
Upvotes: 40
Reputation: 930
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 >=
;
Upvotes: 15
Reputation: 654
How about this one?
return Math.round((Math.random() * 1) + 0) === 0;
Upvotes: -1
Reputation: 60577
For a more cryptographically secure value, you can use crypto.getRandomValues
in modern browsers.
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.
Upvotes: 19