godot icon indicating copy to clipboard operation
godot copied to clipboard

Include random number generators for probability distributions to GDScript

Open lukaskotik opened this issue 10 years ago • 11 comments

Now we can generate numbers from discrete uniform distribution (randi) and from continuous uniform distribution (randf). What about to include generator for other distributions? - http://www.cplusplus.com/reference/random/

Having normal, exponential, Poisson, binomial, Bernoulli can be quite useful. Of course that random number from all these distribution can be generated from an uniform distribution but algorithms usually have high time complexity.

lukaskotik avatar Jul 22 '16 10:07 lukaskotik

This could be nicely done with a Random class, so you can have multiple generators with different parameters each, such as the distribution type but also the seed.

Zylann avatar Jul 22 '16 12:07 Zylann

+1 to OP.

PLyczkowski avatar Sep 24 '16 15:09 PLyczkowski

Not sure if the use case is the same, but I found that when you want a more custom probability distribution, the simplest and most efficient way to achieve this is to use a shuffle bag.

On Sat, Sep 24, 2016 at 12:10 PM, PLyczkowski [email protected] wrote:

  • 1 to OP.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/godotengine/godot/issues/5852#issuecomment-249369552, or mute the thread https://github.com/notifications/unsubscribe-auth/AF-Z20VgmcJPgZtCqfFbhtKCQLoxp-zqks5qtT1xgaJpZM4JSp8T .

reduz avatar Sep 24 '16 15:09 reduz

https://gamedevelopment.tutsplus.com/tutorials/shuffle-bags-making-random-feel-more-random--gamedev-1249

reduz avatar Sep 24 '16 15:09 reduz

I used to control distribution with a ProbabilityField class that allowed to apply an arbitrary frequency curve. I think that should not be difficult to implement in GDScript :)

Zylann avatar Sep 24 '16 16:09 Zylann

From my point of view the Shuffle Bag is even not (pseudo)random generator from a (discrete) distribution. It is less random than standard random generators - see the first discussion post in the link posted by reduz that explains this. My request was to have (pseudo)random generators for the basic continuous and discrete probability distributions. Something like: randn(mu, sigma) to generate random number from Gaussian distribution with mean mu and variance sigma^2 randexp(r) to generate number from exponential distribution with mean r randpo(lambda) to generate number from Poisson distribution with mean lambda etc.

lukaskotik avatar Sep 24 '16 19:09 lukaskotik

ShuffleBag is good when considering finite events, however it's not easily applicable if you want real-numbers distribution. My ProbabilityField class could approach any distribution function, if you can give it a function (gaussian would be based on 1/(x^2+1) for example). The idea is basically to sample the distribution curve at a given resolution, calculate its integral, normalize it and use it as lookup for random numbers. However it's only approximation :p

Zylann avatar Sep 24 '16 20:09 Zylann

Shuffle Bag may be good if you want to control (and reduce) randomness. But don't forget that it is not random generator.

lukaskotik avatar Sep 24 '16 20:09 lukaskotik

For anyone that ends here looking for a gaussian probability generator for GDScript, here it is:

func gaussian(mean, deviation):

    var x1 = null
    var x2 = null
    var w = null

    while true:

        x1 = rand_range(0, 2) - 1
        x2 = rand_range(0, 2) - 1
        w = x1*x1 + x2*x2

        if 0 < w && w < 1:
            break

    w = sqrt(-2 * log(w)/w)

    return floor(mean + deviation * x1 * w)

PLyczkowski avatar Sep 28 '16 12:09 PLyczkowski

Creating Normal (Gaussian) distributions should be possible with a game engine! It is impossible to create a management simulation game without randomized normal distributions...

Lebostein avatar Nov 03 '16 14:11 Lebostein

This is a duplicate/subset of #7199, which is more recent but also got more recent activity, so closing in favour of that one. If some information described here is missing from the other issue, feel free to link it/copy it there.

akien-mga avatar Mar 22 '18 09:03 akien-mga