Thursday, June 6, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  194] [ 6]  / answers: 1 / hits: 97100  / 7 Years ago, mon, july 17, 2017, 12:00:00

I am trying to generate random 0 or 1 as I am writing a script to populdate my database. If it is 1, I will save it as male and 0 the other way around.



Inside my JavaScript:



Math.floor((Math.random() * 1) + 1);


I used this to generate either 1 or 0. However, with the code above, it always return me with 1. Any ideas?


More From » javascript

 Answers
10

You can use Math.round(Math.random()). If Math.random() generates a number less than 0.5 the result will be 0 otherwise it should be 1.


[#57060] Thursday, July 13, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
annaliese

Total Points: 86
Total Questions: 97
Total Answers: 107

Location: Dominican Republic
Member since Sun, Sep 4, 2022
2 Years ago
annaliese questions
;