Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript non-sequential random number generator

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!

like image 496
mrmo123 Avatar asked May 20 '26 10:05

mrmo123


2 Answers

Map your codes and just use a consecutive index anyway:

var codes = [ 65, 83, 68, 70 ];
var index = Math.floor(Math.random()*codes.length);
var random_key = codes[index];  // tada!
like image 111
Halcyon Avatar answered May 23 '26 01:05

Halcyon


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
like image 25
Ignacio Vazquez-Abrams Avatar answered May 23 '26 01:05

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!