Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / random distribution

Author
Message
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 5th Feb 2017 20:27
Hello all,

First post, sorry if I don't quite yet understand the formatting on these posts etc.

Trying to generate random numbers for my game. Problem is I get too many zeroes from Random2(0, 99999). Code:



This gives quite constantly 160 zero values (zeros1). For 10k iterations it is similarly 16, sometimes 17. Sampling from 100k values that is way too much zeroes.

What am I doing wrong?

T
xCept
21
Years of Service
User Offline
Joined: 15th Dec 2002
Location:
Posted: 6th Feb 2017 08:53
This definitely seems like a bug in Random2(). Testing with Random() returns more expected results for each number. I get the same 160 for 0 in your test app, even after setting a manual random seed.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 6th Feb 2017 10:36 Edited at: 6th Feb 2017 10:37
Welcome

It's not just the number zero. I just tested with the same results (161 or 162 each time). But even if you change the first number from zero to 1 or 2, you get the same result

Then it gets weirder.
If you halve the number of possible results (50,000 to 99,999)...you'll still get about 161 occurrences of the first number.
Now take it to a further extreme, just 10% of the possible results (90,000 - 99,999)...and guess what, about 160-65 for the first number!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
PSY
Developer
7
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 6th Feb 2017 10:44 Edited at: 6th Feb 2017 10:45
Confirmed.

It's not about zeros, it's about the lowest value to return.

When I do a val = Random(-20000,99999), it will generate -20000 about 160 times.


(edit) BatVink was faster
PSY LABS Games
Coders don't die, they just gosub without return
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th Feb 2017 11:07
do you used a start seed ?
SetRandomSeed ( seed )
SetRandomSeed2 ( seed )
btw, Val is also used as a command
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
PSY
Developer
7
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 6th Feb 2017 11:26 Edited at: 6th Feb 2017 11:28
Hey Markus,

Using a custom seed doesn't have any influence on the result.

I checked the random distribution of all other numbers, but it turns out it's always the lowest value which occurs more often, and always by a factor of 1.6x10^n

Run this code - it will gather random numbers between 5 and 35 a hundred million times and display the distribution (gonna take some seconds, depending on your machine)
5 will come up about 160.000 times more often than the other numbers, which are distributed equally.




Yes, Val is a bad variable name to use
PSY LABS Games
Coders don't die, they just gosub without return
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 6th Feb 2017 11:31
Reset the random seed every loop, using a random part of the system timer and see if that has an effect.
PSY
Developer
7
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 6th Feb 2017 12:02
Using a random part of the timer won't make it more random, as the random part will be chosen by the already compromised random generator, and the seed has no influence on the distribution of numbers.

As random2 always generates the lowest number more often than any other numbers, every possible seed generated with either random2 or random won't have any effect, as random2 itself is faulty, not the seed generation.
Furthermore, there are times where you need a fixed seed in order to get the same numbers, for example if you want to generate the same maze or the same Worms landscape.
Plus, generating seeds is slow.

Random2 is definitely buggy.
There's probably a bug in the used algorithm, or in the implementation.
PSY LABS Games
Coders don't die, they just gosub without return
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th Feb 2017 12:29
agree,
the first number counts different.
@paul
AGK (Steam) V2017.01.09 : Windows 10 Pro 64 Bit : AMD (16.12.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 6th Feb 2017 18:30
Thanks all for the confirmation and further tests. Still not so familiar with all the vals, incs, dims and whatnot. But if my game works I am happy enough.. Still great to get some advice on it

I tried to come up with a cunning plan to get more uniform distribution that would still work after the bug is fixed and I forget to update the code after. Adapted from PSY:



So I set the upper limit +1, toss the lowest number if given, and reduce 1 from the result. That is assuming that the other numbers are more uniformly distributed. To generate single values, I will then loop that in my own random function until I get a result..

What you think?
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 7th Feb 2017 01:06 Edited at: 7th Feb 2017 01:06
You can probably generate a seed based on GetRawAccelX/Y/Z and GetRawGyroVelocityX/Y/Z if you want a random seed [and you're on a mobile device]. On windows you could randomise it based on mouse movements

My Games - Latest WIP
130,000 installs with AppGameKit and counting
PSY
Developer
7
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 7th Feb 2017 11:59
Quote: "So I set the upper limit +1, toss the lowest number if given, and reduce 1 from the result. That is assuming that the other numbers are more uniformly distributed. To generate single values, I will then loop that in my own random function until I get a result..

What you think?"


You can always use random instead of random2.
It's faster and works better at the moment.
If you have to use random2 because of its range, decrease the lowest number by 1 and drop if it comes up, like:

Let's assume you need a number between -5000 and +5000
repeat
ran = random2 ( -5001, 5000 )
until ran > -5001



@Paul
In the code I posted, replace random2 with random, and you will see that the upcoming numbers ( ignoring the buggy lowest number ) are more homogeneously distributed than with random2.
PSY LABS Games
Coders don't die, they just gosub without return
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 14th Feb 2017 22:16
Thanks, fixed for the next version. Random2 uses a 624 element array as part of its implementation, and the code that updated it was off by one, so every 624 random values it would hit the value that hadn't been updated.
PSY
Developer
7
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 14th Feb 2017 23:04
Awesome,

thanks for the fix AND THE EXPLANATION.

I'm always curious about what caused these kinds of weird bugs


Cheers,
PSY
PSY LABS Games
Coders don't die, they just gosub without return

Login to post a reply

Server time is: 2024-04-26 02:13:24
Your offset time is: 2024-04-26 02:13:24