calculating a self.value, self.randomnum = normalvariate(x, y)
Vincent Davis
vincent at vincentdavis.net
Sat Jun 20 08:54:23 EDT 2009
> # 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
Why not have the def clamp inside the class? I would prefer to keep
everything I need for the class together.
I am new to classes but I have to say things like if __name__ ==
"__main__": have no intuitive meaning to me. It is true I don't know
what __name__ and __main__ do and I can look it up but I don't even
have a guess based on the names and usage.
I am Now not sure if that is what I want or If I want to redraw from
the distribution. I am wanting to simulate test scores. My option see
to be to draw from a normal (I don't want linear) distribution and
scale it to 0-100 or clamp it as you (Xavier) suggested or draw from
the distribution again (this is what I was thinking) I think this is
still what I want but I should look up the implications of each. The
problem I have with the clamp is that the tails in the sample could be
large.
Thanks
Vincent
More information about the Python-list
mailing list