mRand question

Hello,

I have i need to pick a random value from only two choices, so i use this table:

local choices= {1,2}

I seem to get this pattern: 1  1  1  1  1  2

My question is if instead I have the table below, would it make any difference?

local choices= {1,2,1,2,1,2}

Well first, you don’t need a table.  You can do:  local val = math.rand(1, 2) and get a value that’s either 1 or 2.

Now your issue of getting 1 1 1 1 2, you’re running into the nature of random numbers.  Over a short run like that, and only two choices its perfectly normal to get the same number 4 times in a row (or more).  You are in effect flipping a coin.

Over any short run, it’s possible to flip a coin 10 times and get 10 heads.  This is because when you flip a coin there is a 50% chance to get a 1 and a 50% change to get a 2.  You could go for some very long streaks of getting 1’s before you get a 2.   The Law of averages says over a long run, it will average out.  Say you flip the coin 10,000 times you should be very close to a 50/50 split.  But over a run of 10 flips you can’t expect consistency.

Rob

Try this experiment to see what Rob means by long streaks evening out the results:

local distro = {0,0} for i = 1, 1e6 do local j = math.random(1,2) distro[j] = distro[j] + 1 end print(distro[1]) print(distro[2])

Note:  1e6 is shorthand for 1000000.  Then repeat the test several times using for i = 1, 10

The million number runs will have both numbers in the 499,000-501,000 range.  But if you run the 1-10 version many times you will see some (5, 5)'s, some (4, 6)'s and in about 10 runs, I got 3 sets where one number was recorded 8 times and the other number 2.

Rob

Well first, you don’t need a table.  You can do:  local val = math.rand(1, 2) and get a value that’s either 1 or 2.

Now your issue of getting 1 1 1 1 2, you’re running into the nature of random numbers.  Over a short run like that, and only two choices its perfectly normal to get the same number 4 times in a row (or more).  You are in effect flipping a coin.

Over any short run, it’s possible to flip a coin 10 times and get 10 heads.  This is because when you flip a coin there is a 50% chance to get a 1 and a 50% change to get a 2.  You could go for some very long streaks of getting 1’s before you get a 2.   The Law of averages says over a long run, it will average out.  Say you flip the coin 10,000 times you should be very close to a 50/50 split.  But over a run of 10 flips you can’t expect consistency.

Rob

Try this experiment to see what Rob means by long streaks evening out the results:

local distro = {0,0} for i = 1, 1e6 do local j = math.random(1,2) distro[j] = distro[j] + 1 end print(distro[1]) print(distro[2])

Note:  1e6 is shorthand for 1000000.  Then repeat the test several times using for i = 1, 10

The million number runs will have both numbers in the 499,000-501,000 range.  But if you run the 1-10 version many times you will see some (5, 5)'s, some (4, 6)'s and in about 10 runs, I got 3 sets where one number was recorded 8 times and the other number 2.

Rob