Page 1 of 1

Randomness Test Gone Horribly Wrong

Posted: Fri Jun 03, 2016 12:14 pm
by Territan
Now, understand that I've been a stack user since the original Hypercard. Which is why this behavior is so shocking.

I put a single field, named "fldRandomResults" and extended so that I could see at least six lines of it, and a single button, on a card.

The button had the following script on it:

Code: Select all

on mouseUp
   repeat with i = 1 to 6
      put 0 into line i of field "fldRandomResults"
   end repeat
   
   repeat with i = 1 to 1200
      add 1 to line (random(6)) of field "fldRandomResults"
   end repeat
end mouseUp
The idea was to test the randomizer in LiveCode, to see if it was adequate to my needs (and maybe to demonstrate that it's adequate to anyone else's).

Instead, I get results like this:

Code: Select all

180
179
180
181
179
178
While yes, they're very close to each other, and yes, you want to see that in a randomness test, you'll notice that they don't add up to 1200.

What am I doing wrong?

Re: Randomness Test Gone Horribly Wrong

Posted: Fri Jun 03, 2016 12:41 pm
by bn
Hi Territan,

if you change your code to

Code: Select all

on mouseUp
   lock screen
   repeat with i = 1 to 6
      put 0 into line i of field "fldRandomResults"
   end repeat
   
   repeat with i = 1 to 1200
      put random(6) into tRandom
      add 1 to line tRandom of field "fldRandomResults"
   end repeat
   
   put field "fldRandomResults" into tList
   replace cr with comma in tList
   put sum(tList)
   unlock screen
end mouseUp
it works. See output to the message box of the sum of all 6 lines.

Now as to why this does not work for your original script I am clueless. It should work and I would report this as a bug. I tested in 6.7.11 with the same strange result.

Kind regards

Bernd

Re: Randomness Test Gone Horribly Wrong

Posted: Fri Jun 03, 2016 12:46 pm
by LiveCode_Panos
Hi all,

This looks quite similar to bug http://quality.livecode.com/show_bug.cgi?id=7919, which was marked as resolved after the user who originally reported it asked us to close it. I'll reopen it and update the recipe.

Please add your email(s) in the cc list of the bug report, to get progress updates.

Best,
Panos
--

Re: Randomness Test Gone Horribly Wrong

Posted: Fri Jun 03, 2016 12:52 pm
by Klaus
Hi Territan,

1. welcome to the forum! :D

2. you get even more strange result when you do not use a field in the first script, but a variable
and put its content into a field after the second repeat loop!

Code: Select all

on mouseUp
   lock screen
   repeat with i = 1 to 6
      put 0 into line i of ZZZ
   end repeat 
      repeat with p = 1 to 1200
         add 1 to line (random(6)) of ZZZ
      end repeat
      put ZZZ into fld 1
      put ZZZ into tTotal
      replace CR with "," in tTotal
      put sum(tTotal) into fld 2
end mouseUp
You get sometimes more, sometimes less than 1200 as the sum!?

Anyway, the "culprit" is definitively the addressing of the line in the field:
...
add 1 to line (random(6)) of field "fldRandomResults"
...
Which SHOULD be the same as:
...
put random(6) into tRandom
add 1 to line tRandom of field "fldRandomResults"
...
VERY funky indeed! :-D


Best

Klaus

Re: Randomness Test Gone Horribly Wrong

Posted: Fri Jun 03, 2016 2:04 pm
by dunbarx
I made a smaller version of this and stepped through, watching the results:

Code: Select all

on mouseUp
   put "" into field 1
   repeat with i = 1 to 10
      add 1 to line random(6) of field 1
   end repeat
end mouseUp
Sometimes no "entry" into the field is made at all in a pass of the loop, and sometimes a pass adds 2 to a particular line.

I am both perplexed and worried.

Craig Newman

Re: Randomness Test Gone Horribly Wrong

Posted: Sat Jun 04, 2016 11:40 pm
by Territan
bn wrote:Hi Territan,

if you change your code to
-- content snipped --
it works. See output to the message box of the sum of all 6 lines.
With a little independent testing, I stumbled upon the same idea: Put the random value into a bucket first, and use it to pick the line to add to. Accessing the field with the random() function seems to cause it problems.

I wanted to check the results by populating a field and a variable simultaneously, and the only way to do that was to pick the random value first and use it twice. So...

(Also yeah, locking the screen first makes the field fill much faster. Noob move on my part.)

For what it's worth, I vaguely remember the original Hypercard responding to "random line."

Ultimately here's what I came up with:

Code: Select all

on mouseUp
   lock screen -- This just makes things run so much faster.
   
   repeat with i = 1 to 6
      put 0 into line i of field "fldRandomResults"
   end repeat
   
   repeat with i = 1 to 1200
      put random(6) into j
      add 1 to line j of field "fldRandomResults"
   end repeat
   
   -- The Chi-Square Test, for randomness
   put 0 into chi2Calc
   put 0 into fieldTotal
   put field "fldRandomResults" into x
   repeat with i = 1 to 6
      add line i of x to fieldTotal
      subtract 200 from line i of x
      multiply line i of x by line i of x
      add line i of x to chi2Calc
   end repeat
   put sqrt(chi2Calc/200) into chi2Calc
   
   unlock screen
   answer "Sum: " & fieldTotal & return & "Chi value = " & chi2Calc
end mouseUp

Re: Randomness Test Gone Horribly Wrong

Posted: Sun Jun 05, 2016 4:56 pm
by jiml
Just tried
add 1 to line (random(6)) of field "fldRandomResults"
in LC 5.5.0.

The same unexpected behavior occurs.
It seems this is a long time anomaly.
Not good.

The only way around it is to invoke the random function independently rather than within one command line.
This can be done as Bernd showed with:
put random(6) into tRandom
add 1 to line tRandom of field "fldRandomResults"

or more clumsily:
do "add 1 to line" && random(6) && "of field fldRandomResults"

Re: Randomness Test Gone Horribly Wrong

Posted: Sun Jun 05, 2016 5:14 pm
by Klaus
Same anomalies with ANY, which is a bit shorter at least :D

Code: Select all

...
  repeat with i = 1 to 1200
      ## put random(6) into j
      ## add 1 to line j of field "fldRandomResults"
      add 1 to ANY line of field "fldRandomResults"
   end repeat
...

Re: Randomness Test Gone Horribly Wrong

Posted: Thu Jun 09, 2016 3:20 pm
by MaxV
This is absurd, source code:
########CODE#######
on mouseUp
repeat with i=1 to 6
put 0 & return after temp
end repeat
repeat with i = 1 to 1200
add 1 to line (random(6)) of temp
end repeat
put temp into field 1
repeat for each line tLine in temp
add tLine to total
end repeat
put "Total should be 1200, but it's " & total after field 1
end mouseUp
#####END OF CODE#####

I get:

Code: Select all

Total should be 1200, but it's 1248
Total should be 1200, but it's 1356
Total should be 1200, but it's 1239
Total should be 1200, but it's 1152
:shock:

Re: Randomness Test Gone Horribly Wrong

Posted: Fri Jun 10, 2016 5:52 am
by Opaquer
So, I decided to make an extra step in your code Max to let me see each step (I changed it from 1200 to 12 so I wouldn't be waiting for ages :P). I also added a bit that would save the previous result and compare it to the new result each iteration, and through iterations, sometimes it would add 1 (as expected), sometimes it wouldn't add anything (boo), and sometimes it would add more than 1, or even remove up to 3 at a time! This makes no sense whatsoever.

I also set the randomseed to a set amount before testing, and had put random(6) into tRandom like Bernd suggested, and then tried without that to see how the randomseed changed. For every test that I used tRandom, I had the same result, and for every test without tRandom, like in your code, I got the same result, but they were different results!

This is doing my head in so much :(

Re: Randomness Test Gone Horribly Wrong

Posted: Fri Jun 10, 2016 6:00 am
by FourthWorld
Several messages ago Panos flagged this as a bug. Until they fix it the behavior will only remain errant.