Javascript random number generator

Does anyone know how to make an unclassified random number generator in javascript? I know how to make it sequential using Math.floor(Math.random()*11)

where the number will be between 0-10. I'm looking for one that will only spit out 65, 83, 68, 70 (these numbers are the character codes for a, s, d, f ... I'm making a keyboard game). The only other random number generators I've found are biased / non-uniform. If you could give me general direction as to what this is called or even how to do it, that would be greatly appreciated. Many thanks!

+3


source to share


2 answers


Match your codes and just use a sequential index:



var codes = [ 65, 83, 68, 70 ];
var index = Math.floor(Math.random()*codes.length);
var random_key = codes[index];  // tada!

      

+5


source


js> keymap = Array(65, 83, 68, 70);
[65, 83, 68, 70]
js> print(keymap[Math.floor(Math.random()*4)])
65
js> print(keymap[Math.floor(Math.random()*4)])
70
js> print(keymap[Math.floor(Math.random()*4)])
83
js> print(keymap[Math.floor(Math.random()*4)])
65

      



+2


source







All Articles