Random Boolean or Percentage (v7)

Revision 7 of this benchmark created on


Description

Is there a difference between randomize between 0 and 1 and 0 and 100? For those cases where you need only 2 possibilities (like heads and tails).

Test runner

Ready to run.

Testing in
TestOps/sec
Boolean
var ran_number = !! Math.round(Math.random() * 1);

if (!ran_number) {
 return 'lower_than_50pct';
} else {
 return 'higher_than_50pct';
}
ready
Percentage
var ran_number = Math.ceil(Math.random() * 100);

if (ran_number <= 50) {
 return 'lower_than_50pct';
} else {
 return 'higher_than_50pct';
}
ready
round random
var ran_number = Math.round(Math.random());

if (ran_number) {
 return 'lower_than_50pct';
} else {
 return 'higher_than_50pct';
}
ready
!*2<<0
var ran_number = !(Math.random()*2<<0);

if (ran_number) {
 return 'lower_than_50pct';
} else {
 return 'higher_than_50pct';
}
ready
*2<<0
var ran_number = Math.random()*2<<0;

if (ran_number) {
 return 'lower_than_50pct';
} else {
 return 'higher_than_50pct';
}
ready
Math.random()<.5
var ran_number = Math.random()<.5; // Readable, succint

if (ran_number) {
 return 'lower_than_50pct';
} else {
 return 'higher_than_50pct';
}
 
ready
!(Math.random()+.5|0)
var ran_number = !(Math.random()+.5|0); // (shortcut for Math.round)

if (ran_number) {
 return 'lower_than_50pct';
} else {
 return 'higher_than_50pct';
}
 
ready
!(+new Date()%2);
var ran_number = !(+new Date()%2); // faux-randomness

if (ran_number) {
 return 'lower_than_50pct';
} else {
 return 'higher_than_50pct';
}
 
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.