3

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!

1

2 Answers 2

5

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!
Sign up to request clarification or add additional context in comments.

Comments

2
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

Quick, change Array(65, 83, 68, 70) to [65, 83, 68, 70] before someone sees it ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.