vendredi 29 mars 2019

How to create a random number generator function using JavaScript without using the Math.random() function?

I want to create a random number generator function using JavaScript that will return a random number between 0 and 1. Is it possible to create a custom random number generator function without using the Math.random() function?

I tried this approach. It works but I don't know if it is really random or there is any pattern?

var lastRand = 0.5;
function rand(){
    var x = new Date().getTime()*Math.PI*lastRand;
    var randNum = x%1;
    lastRand = randNum;

    return randNum;
}

// Output
for(var n = 0; n < 50; n++){
    console.log(rand());
}

This function depends on the variable lastRand to generate the number. But I want more efficient way to do it.




Aucun commentaire:

Enregistrer un commentaire