0

I have integers in the range of 1..10. I want to choose one based on a distribution of x^2.

What I want is something like:

def random_from_exp(min, max, exponent):
  ...

I'm not familiar with maths or stats and I don't know how to interpret previous answers to this question (lambdas, inversions and whatnot) in the context of my problem above. Please provide a simple explanation with an example.

cyrus
  • 1,338
  • 3
  • 17
  • 26
  • "I want to choose one based on a distribution of x^2." I don't quite understand. Do you want to really generate data on the basis of the distribution of x^2? This isn't commonly referred to as the Exponential Distribution. – Nitish Mar 18 '14 at 22:14
  • Perhaps this isn't clear. What I want is a weighted choice of xs based on x^2. So 5 has a weighting of 25; 10 has a weighting of 100. Would the solution be to generate those weightings in a random weighted choice algorithm? – cyrus Mar 18 '14 at 22:18
  • 1
    What you are looking for is precisely this: http://stackoverflow.com/questions/11373192/generating-discrete-random-variables-with-specified-weights-using-scipy-or-numpy – Nitish Mar 18 '14 at 22:26
  • You might also find this useful: http://stackoverflow.com/questions/14556451/is-there-a-random-number-distribution-that-obeys-benfords-law Instead of squaring a random number, it uses the random number as the power. – Mark Ransom Mar 18 '14 at 22:33
  • @cyrus: Do you want to generate random integers, or floating point values? – Niklas B. Mar 18 '14 at 22:37

1 Answers1

1

If I'm right, you want to simply generate a number that is on the exponential function (x^2) and is between 0 to 10?

That's pretty simple. Get a random number from 0 to the square root of 10 and square it.

import random
import math

print random.uniform(0, math.sqrt(10))**2

Or in your case :

# I have no idea what the exp means. I'm assuming it means exponent.
def random_from_exp(min, max, exp): 
    return random.uniform(min, math.sqrt(max))**exp

This will print (min <= RESULT <= max) ^ exp

gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • Thanks! The distribution of values is actually flipped though (so '10' is selected the least). Subtracting that value from the max seems to work though. – cyrus Mar 18 '14 at 22:48
  • @cyrus The distribution of x^2 when x is uniformly distributed is actually 1/2 1/sqrt(x). Maybe 1 - x^2 works for you anyway. The link given by Nitish above tells how to get exactly the right distribution, if that matters. – Robert Dodier Mar 18 '14 at 23:22
  • The point that I wanted to show here is that if you are able to generate the range of values that you require, then you can feed it to a function. In the case that you want to generate a set of values that have a normal distribution, I believe scikit will surely have a method to do that. – gran_profaci Mar 18 '14 at 23:27
  • This should help you out : http://stackoverflow.com/questions/14266717/random-number-python-with-gaussian-distribution – gran_profaci Mar 18 '14 at 23:28