Dave's Brain

Browse - Programming Tips - How can I make a random boolean value?

Date: 2014oct14
Language: javaScript

Q.  How can I make a random boolean value?

A.  Use Math.random() which returns a number between 0 and 1

function randomBool() { return Math.random() < .5; }
function exampleUse() { for (var i = 0; i < 100; i++) { console.log(randomBool()); } }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.