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.
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.
+1 to OP.
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
.
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 :)
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.
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
Shuffle Bag may be good if you want to control (and reduce) randomness. But don't forget that it is not random generator.
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)
Creating Normal (Gaussian) distributions should be possible with a game engine! It is impossible to create a management simulation game without randomized normal distributions...
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.
Most helpful comment
For anyone that ends here looking for a gaussian probability generator for GDScript, here it is: