LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Randomizer?

Root / Programming Questions / [.]

JoshuawlCreated:
How would I make a randomizer that would randomly select a number. An example would be that I want a number from in between 1 and 8 and it would randomly select 5.

There is a random number generator built in (rng) if you look at the help info on rng it will tell you how to use it. I can't remember off hand. But I know it's there for you.

RND(<max number plus one>) will return a whole integer from 0 to the max num.

To elaborate: RND returns a random integer in the range of 0 to the argument minus 1. So if you did RND(10) it would pick from 0 to 9. In your case, since you want from 1 to 8, you have to do a bit of math reasoning. The RNG only picks from 0 up to (but not including) that bound, so to have it start from 1 you just have to add one. However, there's a simple way to figure this out for any range. In your case, you want 1 to 8. 8 - 1 = 7, so 7 will be the highest number we want returned from our RNG. So, we do RND(8)+1. Pretty simple, and the math works for every range you could possibly want.
WHILE #TRUE
 PRINT RND(8)+1
WEND
If you want a doc that goes into RNG with more detail (including selecting generators and setting seeds) I wrote one.

Ok, thanks