mercredi 30 janvier 2019

How can I make a random function in JavaScript without using the Math.random function?

I want to generate a random number using 0 and 1.

I've written the code, but the larger the number, the larger the error range. How can I write better code?

My current code

Code to generate 0 and 1

const get_zero_or_one = () => {
  return Math.floor(Math.random() * (2))
}


Code to generate a random number using the above code

const RandomResult = (max_number) => {
    let answer = 0;
    for (let i = 0; i < max_number -1; i++) {
        answer += get_zero_or_one()
    }
    return answer
}

The condition is not to use the Math.random function. Thanks!




Aucun commentaire:

Enregistrer un commentaire