Announcement
Collapse
No announcement yet.
Manipulate RANDINT range
Collapse
X

Manipulate RANDINT range
RANDINT By default, generates random number from a uniform distribution of 0 to 2,147,483,647. Is there a way to limit or manipulate this random number generation to produce numbers in the range 1000 to 5000? 
Yes. Assuming you truly want 1000 to 5000 inclusive, then use RANDREAL instead (math people use this all the time):
MATH "TOINT(1000 + (RANDREAL() * 4001))"
RANDREAL generates a number between 0 and 1.0, but NOT INCLUDING 1.0, so basically 0.0 to 0.999999
You do NOT want to round, use TOINT. That way the values will be between 1000 and 5000 inclusive. If you want it to go to 4999, then just change the 4001 to 4000.
Comment

Originally posted by quaizywabbit View Postcompare what it generates and do your own math to it to keep it in range
Any suggestions?
Comment

Generically speaking then, assuming Min is in V10 and Max is in V11 inclusive:
MATH "V10 + TOINT(RANDREAL() * (1 + V11  V10))"
Let's assume it's emulating a die. V10 is 1, V11 is 6. If RANDREAL() is 0.9999, then the calculation will be
1 + TOINT(0.9999 * (1 + 6  1)) or
1 + TOINT(0.9999 * 6) or
1 + TOINT(5.9994) or
1 + 5
or 6
If RANDREAL() is 0.0, then the calculation will be1 + TOINT(0.0 * (1 + 6  1)) or1 + TOINT(0.0 * 6) or
1 + TOINT(0.0) or1 + 0 or
1
Mathematicians do this kind of uniform random distribution all the time when doing simulations.Last edited by franji1; 09052019, 01:07 PM.
Comment

Originally posted by franji1 View PostYes. Assuming you truly want 1000 to 5000 inclusive, then use RANDREAL instead (math people use this all the time):
MATH "TOINT(1000 + (RANDREAL() * 4001))"
RANDREAL generates a number between 0 and 1.0, but NOT INCLUDING 1.0, so basically 0.0 to 0.999999
You do NOT want to round, use TOINT. That way the values will be between 1000 and 5000 inclusive. If you want it to go to 4999, then just change the 4001 to 4000.
Comment

Originally posted by franji1 View PostGenerically speaking then, assuming Min is in V10 and Max is in V11 inclusive:
MATH "V10 + TOINT(RANDREAL() * (1 + V11  V10))"
Let's assume it's emulating a die. V10 is 1, V11 is 6. If RANDREAL() is 0.9999, then the calculation will be
1 + TOINT(0.9999 * (1 + 6  1)) or
1 + TOINT(0.9999 * 6) or
1 + TOINT(5.9994) or
1 + 5
or 6
If RANDREAL() is 0.0, then the calculation will be1 + TOINT(0.0 * (1 + 6  1)) or1 + TOINT(0.0 * 6) or
1 + TOINT(0.0) or1 + 0 or
1
Mathematicians do this kind of uniform random distribution all the time when doing simulations.
Comment

Originally posted by RockB View Post
The Math will depend on the output and may needs to change everytime. It would be better if there a mapping function on domore, say I want to map 0 > 2,147,483,647 to 1000 > 5000.
Any suggestions?
 1 like
Comment
Comment