Randomness Test Gone Horribly Wrong

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Territan
Posts: 2
Joined: Fri Aug 04, 2006 11:22 pm
Location: Glen Burnie, MD

Randomness Test Gone Horribly Wrong

Post by Territan » Fri Jun 03, 2016 12:14 pm

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?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Randomness Test Gone Horribly Wrong

Post by bn » Fri Jun 03, 2016 12:41 pm

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

LiveCode_Panos
Livecode Staff Member
Livecode Staff Member
Posts: 818
Joined: Fri Feb 06, 2015 4:03 pm

Re: Randomness Test Gone Horribly Wrong

Post by LiveCode_Panos » Fri Jun 03, 2016 12:46 pm

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
--

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Randomness Test Gone Horribly Wrong

Post by Klaus » Fri Jun 03, 2016 12:52 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Randomness Test Gone Horribly Wrong

Post by dunbarx » Fri Jun 03, 2016 2:04 pm

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

Territan
Posts: 2
Joined: Fri Aug 04, 2006 11:22 pm
Location: Glen Burnie, MD

Re: Randomness Test Gone Horribly Wrong

Post by Territan » Sat Jun 04, 2016 11:40 pm

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

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Randomness Test Gone Horribly Wrong

Post by jiml » Sun Jun 05, 2016 4:56 pm

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"

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Randomness Test Gone Horribly Wrong

Post by Klaus » Sun Jun 05, 2016 5:14 pm

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
...

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Randomness Test Gone Horribly Wrong

Post by MaxV » Thu Jun 09, 2016 3:20 pm

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:
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Opaquer
Posts: 247
Joined: Wed Aug 14, 2013 8:24 am

Re: Randomness Test Gone Horribly Wrong

Post by Opaquer » Fri Jun 10, 2016 5:52 am

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 :(

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9824
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Randomness Test Gone Horribly Wrong

Post by FourthWorld » Fri Jun 10, 2016 6:00 am

Several messages ago Panos flagged this as a bug. Until they fix it the behavior will only remain errant.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”