Maybe this question is a little stupid but... is this code 100% reliable?
var fortunes = [0,1,2,3,4,5,6];
var pick = fortunes[Math.floor(Math.random() * fortunes.length)];
Let me explain my point. If I understand it correctly, Math.random() returns a number from 0 to 1, both included. So, it is very unprovable but it is possible that it returns 1. Therefore, in the following code, we would be selecting the 8th element (that doesn't exist) as Math.floor( 1 * 7 ) = 7 ( fortunes[7] doesn't exist).
Aucun commentaire:
Enregistrer un commentaire