vendredi 29 mai 2015

Math.random in regards to arrays

I am confused about how arrays (in Javascript) work in tandem with functions like math.random. Here is an example of the code I would be using (I have read on several sites that math.random is not the best pseudorandom number generator but it will work for my purposes of understanding how math.random selects variables in arrays):

var examples= [1, 2, 3, 56, "foxy", 9999, "jaguar", 5.4, "caveman"];
var example= examples[Math.round(Math.random() * (mathematics.length-1))];
console.log(example);

When I put this in to the console log it select one of the variables and print it out there. And since Math.random is selecting a number greater than or equal to 0 and less than 1, what specific number is assigned to each variable in the 'var examples'? Like, what number would be selected that would cause '1' to be printed out or '"foxy"' to be printed out? Does every variable in the array take up an equal amount of the length from greater than or equal to 0 and less than 1? Thanks for the help and sorry if this is worded badly or not in the correct format. This is my first post and I am very new to programming. :)




Aucun commentaire:

Enregistrer un commentaire