calculating a self.value, self.randomnum = normalvariate(x, y)

Xavier Ho contact at xavierho.com
Sat Jun 20 05:37:20 EDT 2009


While there are a lot of valid ways to do it, anything you do will change
the outcome of the probability anyway. I'm assuming you are just looking to
clamp the values.

Try this:

http://codepad.org/NzlmSMN9 (it runs the code, too)

==========================================
# Clamp a normal distribution outcome

import random

class applicant():
    def __init__(self, x, y):
        self.randomnum = clamp(random.normalvariate(x, y), 0, 100)

def clamp(input, min=0, max=100):
    """Clamps the input between min and max.

    if  input < min, returns min
        min < input < max, returns input
        input > max, returns max

    Default: min = 0, max = 100."""
    if input < min:
        return min
    elif input > max:
        return max
    else:
        return input

if __name__ == "__main__":
    for num in range(10):
        print applicant(random.randint(0,100),
random.randint(0,100)).randomnum
======================================================

Or you could just use randint() if you only wanted a linear distribution.

PS: Thanks, btw, new to python myself also, and looking into this was cool.
:]

Best regards,

Ching-Yun "Xavier" Ho, Technical Artist

Contact Information
Mobile: (+61) 04 3335 4748
Skype ID: SpaXe85
Email: contact at xavierho.com
Website: http://xavierho.com/


On Sat, Jun 20, 2009 at 3:20 PM, Vincent Davis <vincent at vincentdavis.net>wrote:

> I currently have something like this.
>
> class applicant():
>    def __int__(self, x, y):
>        self.randomnum = normalvariate(x, y)
> then other stuff
>
> x, y are only used to calculate self.randomnum   and this seems to
> work. But I want self.randomnum to be 0 <= randomnum <= 100. The only
> way I can thing of to do this is is with a while statement and that
> seems more complicated than necessary. I would really like to keep it
> on one line. How would I do that?
>
> Thanks
> Vincent Davis
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090620/863dd029/attachment-0001.html>


More information about the Python-list mailing list