Generating a random number (GRX)
Brian K. White
brian at aljex.com
Wed May 12 15:18:17 PDT 2010
On 5/12/2010 12:14 PM, Chris Sellitto wrote:
> filePro® version 5.0.14RN9
> Windows Server/XP environments.
>
> I am trying to generate a number between 1-20, and seem to be doing something wrong. I keep generating the same number over and over. Below is a small test I tried, and RN always displays as 2192.
>
> 223 ------- - - - - - - - - - - -
> @keyR ◄ If:
> Then:
> 224 ------- - - - - - - - - - - -
> ◄ If: rn eq ""
> Then: rn(4,.0)=rand("20")
> 225 ------- - - - - - - - - - - -
> ◄ If:
> Then: rn=rand(" ")
> 226 ------- - - - - - - - - - - -
> ◄ If:
> Then: msgbox "Random Number ="<rn
> 227 ------- - - - - - - - - - - -
> ◄ If:
> Then: end
>
> Any help is always appreciated.
>
> Thanks in advance.
>
> Christopher Sellitto
> VP Computer Operations
> Guaranteed Returns®
> sellich at guaranteedreturns.com
> _______________________________________________
> Filepro-list mailing list
> Filepro-list at lists.celestial.com
> http://mailman.celestial.com/mailman/listinfo/filepro-list
>
[...]
Then: rn = (rand()/("32767"/"19"))+"1"
[...]
@once
Then: x = rand("-1")
Then: end
The math comes from the fact that rand() will output from 0 to 32767.
In order to convert 0-32767 to 1-20, you want to divide 32767 by 19 (not
20), then divide rand() by that, which will give you a result from 0 to
19, then just add 1 to that for 1-20.
The @once is how you seed rand(). You only want to seed it once, and
"-1" means to use the system time to seed it, so each time someone runs
this process, they each seed rand() differently.
You can sort of think of rand() as a fixed written down list of random
numbers, and each time you call rand() you are just reading the next
number from that list, but the list is always the same and the first
time you run rand() per process you are always starting out at the same
point at the top of the list. You can sort of think of seeding rand() as
changing the position in the list where you will start reading from
instead of always starting at the top.
If you never seed, or if you always seed with the same value, you'll
always get the same number from the first call to rand() and the same
next number, and the same next number...
I don't think rand() is _actually_ returning data from a fixed list,
more like running some math algorithm that will always produce the same
output given the same starting input. I'm just saying that is a way to
think about the purpose and effects of seeding, why you always want to
do it, why you only need to do it once per session, and why you want to
supply your own bit of randomness (system time) for the seed so you
always seed differently.
--
bkw
More information about the Filepro-list
mailing list