Generating a random number from n to m
eziosoma 08 Apr 2013 21:39
If you want to create a pseudo-function that generates random numbers from a 16 bit generic signed integer n to a generic 16 bit signed integer m, with m strictly greater than m and m-n lower than 32767, you can use the following code:
# declaring pseudofunction input and output arguments.
var myrand_n #input
var myrand_m #input
var myrand_res #output
var x
# declaring pseudofunction myrand
sub myrand
call math.rand(myrand_res)
myrand_res = abs(myrand_res % (myrand_m+1 - myrand_n)) + myrand_n
# Now You can call pseudofunction myrand when required
# for example:
onevent button.forward
# initialize arguments: m must be greater then n and m-n must be lower than 32767
myrand_m=1000 #only values between -32768 and 32767 are allowed
myrand_n=100 #only values between -32768 and 32767 are allowed
callsub myrand
x = myrand_res # The final result will be in the variable x
Sic Parvis Magna