jeudi 6 avril 2017

Understanding the use of Math.floor when randomly accessing an array

I used a code I found on this site to access an array of string values. I changed the variable names, but other than that, the code remains the same.

var rand = array[Math.floor(Math.random() * array.length)];

It works, and as I understand it, (Math.random() * array.length) is the area which generates the random number itself, so why is Math.floor required? I am clearly not understanding something quite obvious here.




Aucun commentaire:

Enregistrer un commentaire