### Issue with random number generation

#### Gjam25

• Posts: 26
Hey guys, I have a question about RNG.  Right now in a game I'm working on I have  a certain percent chance that something will happen based on a character's stat.  Basically, if the stat < Random Number between 1 and 100 , then the event happens.  So the higher the stat, the less likely the event.

The issue I'm seeing is that even if the stat has a very high value, let's say 80, the amount of times that the event happens seems to be statistically unlikely.  In theory, if a random number between 1 and 100 has to be higher than 80 for this event to happen, the event should only be happening close to 20% of the time.  In practice, it seems to be happening more often than chance would allow - maybe like 4 times out of 10.  It seems like things get even more out of whack as the stat goes lower:  for example at 50, when it should be a 50/50 split as to whether the event happens, it seems like it will happen 3,4,5 times in a row.

Has anyone else had issues like this using random numbers to assign a % chance of something happening?  Are there better ways of doing that, or adjusting the random number approach to be more accurate?

#### JeffreyDriver

• Posts: 2168
I'm not sure how Stencyl generates random numbers, but I'm pretty sure that you're just seeing patterns (look up apophenia) How big is your sample? I'm sure that if you ran it hundreds or thousands of times you'd see it even out. Having a 50% chance come out with one answer 5 times in a row doesn't seem strange to me.
Current Projects:
The Grand Grimoire Chronicles Episode 1 now on Kongregate!.
I always appreciate useful feedback. Thank you.
www.jeffreydriver.co.uk

#### Gjam25

• Posts: 26
You could be right;  I haven't run it with a very big number of samples yet - more than 100 times anyway.  it might just be me nitpicking lol.

#### merrak

• Posts: 2524
In theory, ...

The law of large numbers only applies if the sample size is "large"

Your result doesn't sound so surprising, but if you're concerned about the quality of the random number generator, it'd be easy to run a little experiment and see if you get the desired frequency eventually.

#### Exportforce

• Posts: 90
You can not tell if a random-number function works or not with just a few tries, not even with 500 tries. You still can easily get bigger chunks of a single number or a "small" range of e.g. "high tier" numbers. Create yourself a little loop that creates an output of 10.000 numbers from 0 - 100 and store them into variables (I guess "lists" is best for that) and give yourself just the output of HOW OFTEN each number came out and how much percent of all numbers that was. With that you'll have a better view of the ranges or if there really (which I don't really think) is some bug with it.

#### Exportforce

• Posts: 90
For a better understanding I wrote a Random Number Generator. You can roll 1-10 from 1 to 1.000.000 times and see how often each number has been rolled incl. their %
You will see the higher the rolls are the more even are the %. Showing you that you can't make out if a random generator works by just a few rolls. Same accounts to droprates in games (Like Diablo or World of Warcraft). An item can have a dropchance of 0.0001% and one player can find that item in a short time of maybe 10 kills 2 times or even, when he's lucky, 2 kills = items.

Small tipp: Depending on your CPU the number generator can take up to 10 seconds for the 1.000.000 rolls. So be patient a bit.

Have fun