does anyone know how to make a non-sequential random number generator in javascript? I know how to do a sequential one using Math.floor(Math.random()*11) where the number will fall in 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 ones random number generators I've found are biased/non-uniform ones. If you could give me a general direction as to what this is called or even how to make on, it'd be greatly appreciated. Thanks so much!
-
1Here is an interesting source on that: baagoe.com/en/RandomMusings/javascriptEran Medan– Eran Medan2012-03-17 00:42:05 +00:00Commented Mar 17, 2012 at 0:42
Add a comment
|
2 Answers
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
1 Comment
Dagg Nabbit
Quick, change
Array(65, 83, 68, 70) to [65, 83, 68, 70] before someone sees it ;)