Stuper Simple Ques - Random #'s
Agent420
Posts: 439
I feel foolish that I can't figure this out after consulting both the manual and a forum search, but...
What is the best method to compute a random number for a desired range.· I'm not concerned about 'true' randomness, I just want to understand the ? operator.· I understand seed values and forward and backward sequences.
The manual states that the ? operator uses a variable's value as the seed and generates and number from NEGX to POSX.·
Say I want a random value from 1 to 10.· In the old Basic days, rnd returned a value from 0->1 that you simply mutliplied by the desired range.· Here it seems you would have to bit AND then modulus or something to scale the returned random value.
TIA
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What is the best method to compute a random number for a desired range.· I'm not concerned about 'true' randomness, I just want to understand the ? operator.· I understand seed values and forward and backward sequences.
The manual states that the ? operator uses a variable's value as the seed and generates and number from NEGX to POSX.·
Say I want a random value from 1 to 10.· In the old Basic days, rnd returned a value from 0->1 that you simply mutliplied by the desired range.· Here it seems you would have to bit AND then modulus or something to scale the returned random value.
TIA
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Comments
result = ?result // 10
Gives values 0 to 9 so just add 1.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I had to learn this the hard way, with a program that generated 1 random number for every person on an art exibition. Because the first 5 numbers were all positiv, all my test runned OK, but then at the exibition the desaster started after a few persons.... (I got negative indexes into an array).
Andy
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
I ask the same question a month or so ago.
All I needed was a bump in the right direction.
Roger
You could also shift it right one bit, e.g. result := (?seed >> 1) // 10 which might take less token space since you don't need the 32 bit constant.
Or, probably even shorter, you could just take its absolute value before using it: result := (||(?seed))//10 Some of those parentheses might not be necessary, I'm doing this off the top of my head.
On the subject of Random, I wrote this blog entry a year ago:
http://it.toolbox.com/blogs/codesharp/more-random-than-random-25106
Edit to add:
Fun exercise with random numbers
http://stackoverflow.com/questions/804316/how-do-i-implement-rand7-in-terms-of-rand5
Post Edited (NetHog) : 9/12/2009 2:18:07 AM GMT