bad interaction between "random" and "word"?

Michael Lew michaell at unimelb.edu.au
Mon Apr 6 18:51:13 EDT 2009


Mick

I too have been playing with random numbers (I do lots of statistical
programming with Rev, so random(0 is one of my tools).

Your problem sound a very much like it relates to a similar issue that I
came across with random() and arrays a few days ago (quoted below).

I'm pretty sure now that it represents a bug -- a very scary one too,
because it would be completely invisible in many circumstances. Do you want
to post a bug report?

Mike

My post:
> I have struck an unexpected issue with an array. I assumed that the two
> following code snipets would be equivalent, but they are not.
> 
> Version 1 
>     put random(10)-1 into tnum
>     add 1 to iHisto[tnum]
> Version 2
>     add 1 to iHisto[(random(10)-1)]
> 
> Version 1 does what I expected, and, I guess is more readable and so
> preferable for that reason. However, I can't see why the two versions differ
> in their effects.
> 
> Here is the full code of my button for anyone who wishes to try it out (the
> different versions are indicated with comments):
> 
> on mouseUp pMouseBtnNo
>     put 10000 into oreps
>     put 100 into ireps
>     set the cursor to busy
>     repeat oreps
>         put 0 into kount
>         repeat ireps
>             add 1 to kount
>             -- **next two lines
>             -- put random(10)-1 into tnum
>             -- add 1 to iHisto[tnum]
>             -- **OR the next one line
>             add 1 to iHisto[(random(10)-1)]
>             --
>         end repeat --inner
>         combine iHisto by return and comma
>         sort lines of iHisto numeric descending by item 2 of each
>         add 1 to ohisto[item 2 of line 1 of iHisto]
>         put empty into iHisto
>     end repeat --outer
>     combine oHisto with return and tab
>     sort lines of oHisto numeric ascending by word 1 of each
>     put oHisto into fld "OutputFld"
> end mouseUp
> 
> Thanks,
> Michael Lew

Original post:
> 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?




More information about the use-livecode mailing list