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

Dave Angel davea at
Sat Jun 20 09:57:53 EDT 2009

Vincent Davis 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
There are at least two interpretations to your requirement, both of 
which change the distribution.  So you have to specify what the real 
distribution is that you want.

  1) clamp - see Xavier's response.  This will give you more values at 
exactly 100 and/or exactly 0 than normalvariate would specify,
  2) discard - this will throw away any values outside the range, and 
try again.  And in this case, a loop is essential, plus some mechanism 
to handle the case where the loop would never terminate.  For example, 
suppose x is 10000.


More information about the Python-list mailing list