0

let's say I want to pull out random values from a linear distribution function, I'm not sure how I would do that.. say I have a function y = 3x then I want to be able to pull out a random value from that line. this is what I've tried:

 x,y = [],[]
 for i in range(10):
    a = random.uniform(0,3)
    x.append(a)
    b = 3*a
    y.append(b) 

This gives me y values that are taken from this linear function (distribution per say). Now if this is correct, how would I do the same for a distribution that looks like a horizontal line?

that is what if I had a horizontal line function y = 3, how can I get random values pulled out from there?

6
  • 1
    You'll need a better understanding of what a distribution is to solve your problem, because what you have is not a distribution. Commented Sep 4, 2014 at 22:34
  • Cumulative distribution functions are indeed defined in this way. I'm not sure if you have a clear understanding of my question dear comrade. Commented Nov 30, 2014 at 2:13
  • A cumulative distribution function is required to have values in the range [0, 1]. y = 3x doesn't fit that criterion. Commented Nov 30, 2014 at 2:54
  • cumulative distribution is defined as F(x) = x for x in [0,1]. Adding a scaling factor of 3 doesn't do any harm to the pseudo-random nature of the distribution function. would only change the limits. No big deal. itl.nist.gov/div898/handbook/eda/section3/eda3662.htm Commented Nov 30, 2014 at 2:59
  • Do acknowledge that you have a point :) Commented Nov 30, 2014 at 3:07

1 Answer 1

1

Just define your function, using a lambda or explicit definition, and then call it to get the y-value:

def func(x):
    return 3

points = []
for i in range(10):
    x = random.uniform(0, 3)
    points.append((x, func(x))

A linear function with a slope of 0 in this case is fairly trivial.

EDIT: I think I understand that question a little more clearly now. You are looking to randomly generate a point that lies under the curve? That is quite tricky to directly calculate for an arbitrary function, and you probably will want a bound to your function (i.e a < x < b). Supposing we have a bound, one simple method would be to generate a random number in a box containing the curve, and simply discard it if it isn't under the curve. This will be perfectly random.

def linearfunc(x):
    return 3 * x

def getRandom(func, maxi, a, b):
    while True:
        x = random.uniform(a, b)
        y = random.uniform(0, maxi)
        if y < func(x):
            return (x, y)

points = [getRandom(linearFunc, 9, 0, 3) for i in range(10)]

This method requires knowing an upper bound (maxi) to the function on the specified interval, and the tighter the upper bound, the less sampling misses will occur.

Sign up to request clarification or add additional context in comments.

5 Comments

but aren't the values in the array points same as x? just a random uniform bunch of values between 0 and 3. how can I ensure it's from the function?
the output i want is just an array with single element values that are solution to the function mentioned.
similar to how numpy.random.normal(mu,sigma,size) gives single element values array of the specified size that are acquired from the normal distribution specified, I'm looking to get values analogous to this.
I responded, but I'm not 100% sure I'm completely understanding what you're looking for. Let me know if this isn't it
So yes, I do have bounds 0 to 3 specified. I think your updated solution is on it's way. I've rephrased the question here to better explain it. I don't want values from under the curve, I want random values FROM the curve, or from the LINE in this case. Thank you very much for your help regardless!! :) stackoverflow.com/questions/27209244/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.