Page 1 of 1

random number generator issue

Posted: Thu Aug 12, 2021 11:29 pm
by rcmills
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

Re: random number generator issue

Posted: Fri Aug 13, 2021 12:26 am
by bn
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

Re: random number generator issue

Posted: Fri Aug 13, 2021 1:17 am
by rcmills
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.

Re: random number generator issue

Posted: Fri Aug 13, 2021 9:51 am
by richmond62
none of these sets of 10 numbers add up to 10,000
Why should they if they are random?

Re: random number generator issue

Posted: Fri Aug 13, 2021 10:00 am
by bn
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