godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
88.49k stars 20.05k forks source link

Include random number generators for probability distributions to GDScript #5852

Closed lukaskotik closed 6 years ago

lukaskotik commented 8 years ago

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.

Zylann commented 8 years ago

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.

PLyczkowski commented 7 years ago

+1 to OP.

reduz commented 7 years ago

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 notifications@github.com 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 commented 7 years ago

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

Zylann commented 7 years ago

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 :)

lukaskotik commented 7 years ago

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.

Zylann commented 7 years ago

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

lukaskotik commented 7 years ago

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

PLyczkowski commented 7 years ago

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)
Lebostein commented 7 years ago

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

akien-mga commented 6 years ago

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.