Random Boolean or Percentage (v4)

Revision 4 of this benchmark created by Artur Barcicki 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
Boolean2
var ran_number = (Math.random() > 0.5);

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

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.