random number generator issue

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
rcmills
Posts: 42
Joined: Wed Nov 21, 2018 8:27 pm

random number generator issue

Post by rcmills » Thu Aug 12, 2021 11:29 pm

I was doing some simulations, and using the random() function, and got some results that I doubted. To do a quick test, I wrote the following simple test, and got these results on several successive runs:

997,997,997,996,998,991,997,997,996,995
1050,1048,1048,1051,1053,1047,1054,1053,1052,1051
1005,1003,1006,1004,1003,1005,1004,1002,1006,1006
934,933,934,934,934,935,930,933,931,931

As you can quickly see, none of these sets of 10 numbers add up to 10,000. This implies to me that some of the random numbers are being placed somewhere other than the item in the list where is should be.

Does this make any sense? How can I trust the random number generator at all??

Code: Select all

on mouseUp
   get 10
   repeat with i = 1 to 1000 * it
      add 1 to item (random (it)) of tList
   end repeat
   put tList
end mouseUp

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

Re: random number generator issue

Post by bn » Fri Aug 13, 2021 12:26 am

Hi rcmills,

I never use it the way you use it. As soon as I get "it" I put it into a variable.
But that does not solve the mystery of your results.

This however works.

Code: Select all

on mouseUp
   get 10
   repeat with i = 1 to 1000 * it
      put random(it) into tRandom
      add 1 to item tRandom of tList
   end repeat
   put tList
   put cr & sum(tList) after msg
end mouseUp
I do not understand why your code does not work. It seems that addressing item x of tList is confused by the function used to generate a number. Random itself works as shown by my code.

Kind regards
Bernd

rcmills
Posts: 42
Joined: Wed Nov 21, 2018 8:27 pm

Re: random number generator issue

Post by rcmills » Fri Aug 13, 2021 1:17 am

Thanks much!

Yes, that works perfectly (of course!). I don't understand why the other method doesn't work. Just another of life's mysteries - lol.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9285
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: random number generator issue

Post by richmond62 » Fri Aug 13, 2021 9:51 am

none of these sets of 10 numbers add up to 10,000
Why should they if they are random?

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

Re: random number generator issue

Post by bn » Fri Aug 13, 2021 10:00 am

richmond62 wrote:
Fri Aug 13, 2021 9:51 am
none of these sets of 10 numbers add up to 10,000
Why should they if they are random?
because only the selection of the bin (item x of tList) is random, not that 10000 times 1 is added to the bins.

Kind regards
Bernd

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”