Search code examples
mathrandomstatisticsdistribution

Unequally/ unevenly distributed random floats


How can I generate random floats between X and Y, that are un-equally distributed, so that it's more likely to generate numbers from a specific range, within X to Y?

I did search a lot of keywords to find something like this including: unequally distributed random numbers, or uneven noise distribution, or biased random floats, or weighted random numbers ...

All I could find is to randomly pick from a bag of finite list of values, weighted so that some values are more likely to be chosen, but I'm looking to choose from an infinite range of floats between X and Y.

Also I found a lot of articles about how to NOT generate biased random numbers, which is the opposite of what I want.

As an example of WHAT I'm trying to do with these numbers: If you draw black noise in a white square, each noise dot is in a random location within the square, if you generate enough dots, you'll have a almost black square.

If you distribute the randomness with a higher probability in the middle of the square, you'll draw almost a soft black dot in the middle of the square. This is what I'm trying to generate.

So my questions are:

  • I'm sure these algorithms exist, how are they called?
  • maybe can anyone suggest a quick implementation, in any language?
  • how can you specify the weight of the bias? Eg: it's 2x more likely, or 5x more likely to generate numbers in a specific range? In my example with the dot, I believe if it's 5x more likely to get numbers in a range, the dot will be smaller and darker.
  • how can you specify the softness of the distribution? Eg: linear, longaritmic, quadratic. In my example with the dot, I believe it will make the dot softer, or harder.

Thank you in advance!


Solution

  • These are generally called absolutely continuous distributions, and the following are two ways to define this kind of distribution.

    For many popular distributions, such as the normal, beta, and gamma distributions, there are special methods for generating random numbers with those distributions. In fact, there are many different designs of such methods for the normal distribution. For numbers in a bounded range, the beta distribution is an ideal choice; its two parameters (alpha and beta) describe a wide variety of shapes that could suit your purposes. Python has a random.betavariate(alpha, beta) method for generating beta-distributed random numbers.