bad interaction between "random" and "word"?

Bernard Devlin bdrunrev at gmail.com
Mon Apr 6 10:46:13 EDT 2009


I believe the same issue might well have come up just a few days ago
in a slightly different context:

http://www.nabble.com/Array-issue-td22859541.html

It does seem that in not putting the result of random() into a
variable before using it, people are seeing strange results.
Personally, I would adopt the more verbose approach, as it would aid
debugging.

Bernard

On Mon, Apr 6, 2009 at 4:24 AM, Mick Collins <mickclns at mac.com> wrote:
> Hello, everyone,
> I was working on a project that involved randomness, which seemed to be a
> little off, so I did some testing.
>
> I ran this handler from msgbox:
> on tst2
>   repeat with i = 2 to 10
>      put i into randarg
>      put "0 0 0 0 0 0 0 0 0 0" into theList
>      put 10000 * i into nRepeats
>      repeat nRepeats times
>         add 1 to word random(randarg) of theList
>      end repeat
>      put nRepeats && randarg && theList into line i of fld "tmpFld"
>   end repeat
> end tst2
>
> THE RESULTS:
>
> 20000 2 10027 10027 0 0 0 0 0 0 0 0
> 30000 3 9974 9973 9974 0 0 0 0 0 0 0
> 40000 4 10152 10153 10151 10150 0 0 0 0 0 0
> 50000 5 10144 10145 10142 10143 10142 0 0 0 0 0
> 60000 6 9981 9981 9979 9982 9979 9980 0 0 0 0
> 70000 7 10188 10193 10191 10187 10188 10192 10191 0 0 0
> 80000 8 10088 10089 10088 10089 10087 10086 10088 10087 0 0
> 90000 9 9984 9978 9976 9984 9983 9980 9980 9983 9983 0
> 100000 10 9998 9998 9997 9998 9999 9999 9998 9998 9998 9998
>
> You can see the numbers of repeats are as expected, yet the sum of each line
> SHOULD be exactly the same as the number of repeats, but isn't (some above,
> some below) ... the individual numbers are all near to 10000 but even nearer
> to one another in a given line (but aren't all equal either).
>
> HOWEVER, when I replace the line
>         add 1 to word random(randarg) of theList
> WITH
>         put random(randarg) into tmp
>         add 1 to word tmp of theList
>
> THE RESULTS ARE:
>
> 20000 2 9825 10175 0 0 0 0 0 0 0 0
> 30000 3 9792 9994 10214 0 0 0 0 0 0 0
> 40000 4 10077 9955 9808 10160 0 0 0 0 0 0
> 50000 5 9926 10083 10078 9888 10025 0 0 0 0 0
> 60000 6 9964 10100 9922 9928 10019 10067 0 0 0 0
> 70000 7 9920 10094 9945 9823 9968 10079 10171 0 0 0
> 80000 8 10143 10129 10080 9809 10024 9948 9971 9896 0 0
> 90000 9 9764 10054 10094 9983 10038 10017 9991 10070 9989 0
> 100000 10 10029 9947 9910 9776 10252 10114 9935 9938 10006 10093
>
> This appears correct ... the sum of each line is (the ones I've checked are
> and estimates of the others are close) the number of repeats and the numbers
> seem randomly sprinkled around 10000.
>
> It seems to me that the replacement lines should do exactly the same thing
> as the line they replace.  Is this not working correctly or am I missing
> some knowledge of how it should be working?
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription
> preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
>



More information about the use-livecode mailing list