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

0 views

### Vincent Davis

Jun 20, 2009, 1:20:22 AM6/20/09
to pytho...@python.org
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

### Vincent Davis

Jun 20, 2009, 8:54:23 AM6/20/09
to pytho...@python.org
> # 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

### Dave Angel

Jun 20, 2009, 9:57:53 AM6/20/09
to Vincent Davis, pytho...@python.org
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.

### Steven D'Aprano

Jun 20, 2009, 10:35:19 AM6/20/09
to
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.

Then it isn't a normal variate.

> The only
> way I can thing of to do this is is with a while statement and that
> seems more complicated than necessary.

Why? It's a perfectly simple procedure:

def __int__(self, x, y):
x = -1
while not 0 <= x <= 100:
x = normalvariate(x, y)
# do other stuff

That is the correct way to truncate a normal distribution.

Alternatively, truncate values past the end-points, but this will distort
the random distribution significantly:

x = max(0, min(100, normalvariate(x, y)))

You probably don't want that, as it will cause far more 0 and 100 values
than you would otherwise expect.

> I would really like to keep it
> on one line. How would I do that?

Why? Is the enter key on your keyboard broken?

--
Steven

### Steven D'Aprano

Jun 20, 2009, 10:54:22 AM6/20/09
to
Vincent Davis wrote:

>> # Clamp a normal distribution outcome

I don't know who you are quoting -- you should give attribution to them.

>> def clamp(input, min=0, max=100):
...

>> if input < min:
>> return min
>> elif input > max:
>> return max
>> else:
>> return input

An easier way to do this:

return min(100, max(0, input))

but again, I stress that this will strongly distort the random distribution.
It's probably not what you want.

> Why not have the def clamp inside the class?

Why bother?

> I would prefer to keep
> everything I need for the class together.

But you don't. You have the random.normalvariate in a completely different
module. I'm sure you have other operations like +, - etc as built-in
functions. Not everything is inside the class.

> 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.

When you import a module with the line:

import mymodule

Python automatically creates a variable mymodule.__name__ and sets it to the
string "__mymodule__".

When you run a module as a script, by calling it from the shell, using (for
example):

\$ python mymodule.py

Python automatically creates the variable mymodule.__name__ as before, but
this time sets its value to the string "__main__".

So the construction:

if __name__ == "__main__":
do_stuff_here()

is a way to include code that will only be executed when running the module
as a script, not when it is imported as a module.

> 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.

Strictly speaking, you want a different distribution, not normal. Possibly
the F-distribution? Anyway, whatever it is, unless you need very accurate
results, a truncated normal distribution shouldn't be *too* far off: close
enough for government work. Clamping will not be very good: it will result
in an excess of 0 and 100 scores. Imagine a graph that looks vaguely like
this:

000: **********
010: *
020: **
030: ****
040: *******
050: *********
060: **********
070: ********
080: *****
090: **
100: *************

That's what you'll get by clamping (possibly exaggerated for effect).

Truncating with a while loop will result in something closer to this:

000: *
010: *
020: **
030: ****
040: *******
050: *********
060: **********
070: ********
080: *****
090: **
100: *

which is far less distorted.

--
Steven

Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

### Piet van Oostrum

Jun 21, 2009, 8:25:13 AM6/21/09
to
I notice that I see several postings on news:comp.lang.python that are
replies to other postings that I don't see. Examples are the postings by
Dennis Lee Bieber <wlf...@ix.netcom.com> that I am replying to (but I
break the thread on purpose). For example the posting with Message-ID:
<mailman.1883.1245538...@python.org> references:
<77e831100906192220y553...@mail.gmail.com> which is
not present on my news server. I have been wondering why these
disappear, and I noticed the following in the Dennis Lee Bieber posting:
On Sat, 20 Jun 2009 11:48:21 -0600, Vincent Davis
<vin...@vincentdavis.net> declaimed the following in
gmane.comp.python.general:

So apparently some of these come through gmane.comp.python.general.

So my question is: would this be the cause of these disappearing
postings? Are postings on gmane.comp.python.general not relayed to
comp.lang.python? Are they relayed to the python mailing list? I find it
quite disturbing that sometimes only half of a discussion is visible.
--
Piet van Oostrum <pi...@cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: pi...@vanoostrum.org

### Chris Rebert

Jun 21, 2009, 2:22:39 PM6/21/09
to Piet van Oostrum, pytho...@python.org
On Sun, Jun 21, 2009 at 5:25 AM, Piet van Oostrum<pi...@cs.uu.nl> wrote:
> I notice that I see several postings on news:comp.lang.python that are
> replies to other postings that I don't see. Examples are the postings by
> Dennis Lee Bieber <wlf...@ix.netcom.com> that I am replying to (but I

As addressed in an earlier thread, Mr. Bieber chooses to set a Usenet
header (X-Noarchive) in his postings that suppresses their permanent
archiving (and often the archiving of replies to his posts).

I would direct you to the thread, but it looks like it wasn't archived. :P

Cheers,
Chris
--
http://blog.rebertia.com

### Piet van Oostrum

Jun 21, 2009, 3:03:54 PM6/21/09
to
>>>>> Chris Rebert <cl...@rebertia.com> (CR) wrote:

>CR> On Sun, Jun 21, 2009 at 5:25 AM, Piet van Oostrum<pi...@cs.uu.nl> wrote:
>>> I notice that I see several postings on news:comp.lang.python that are
>>> replies to other postings that I don't see. Examples are the postings by
>>> Dennis Lee Bieber <wlf...@ix.netcom.com> that I am replying to (but I

>CR> As addressed in an earlier thread, Mr. Bieber chooses to set a Usenet
>CR> header (X-Noarchive) in his postings that suppresses their permanent
>CR> archiving (and often the archiving of replies to his posts).

>CR> I would direct you to the thread, but it looks like it wasn't archived. :P

Actually, I do see Mr. Bieber's posts, but not the ones he replies to.
And I am not talking about the archives but about the regular NNTP
server, although the retention period somewhere along the path could be
so low that posts disappear before they reach us. Anyway, as I saw gmane
mentioned in the replies I wondered if that would have something to do
with the problem.

### Terry Reedy

Jun 21, 2009, 4:16:10 PM6/21/09
to pytho...@python.org
Piet van Oostrum wrote:
> I notice that I see several postings on news:comp.lang.python that are
> replies to other postings that I don't see. Examples are the postings by
> Dennis Lee Bieber <wlf...@ix.netcom.com> that I am replying to (but I
> break the thread on purpose). For example the posting with Message-ID:
> <mailman.1883.1245538...@python.org> references:
> <77e831100906192220y553...@mail.gmail.com> which is
> not present on my news server. I have been wondering why these
> disappear, and I noticed the following in the Dennis Lee Bieber posting:
> On Sat, 20 Jun 2009 11:48:21 -0600, Vincent Davis
> <vin...@vincentdavis.net> declaimed the following in
> gmane.comp.python.general:
>
> So apparently some of these come through gmane.comp.python.general.

I am posting and reading thru gmane and generally see no problem.
Sometimes I do see replies before the posting being replied.

Sometimes certain posts get 'echoed' about week after the original
posting date. If a reply gets echoed, but not the original, and one
missed both originally, that can look weird.

tjr

Message has been deleted

### Steven D'Aprano

Jun 21, 2009, 7:24:28 PM6/21/09
to
Piet van Oostrum wrote:

> I notice that I see several postings on news:comp.lang.python that are
> replies to other postings that I don't see.

I see the same problem.

I suspect it's because of over-vigorous spam filtering from Usenet
providers. Some even block everything from anyone using Google Groups. It's
quite frustrating, to have perfectly valid Python-related posts go missing
while dozens of posts offering to sell well-known brands of shoes and
watches are delivered.

--
Steven

### greg

Jun 21, 2009, 8:44:47 PM6/21/09
to
Dennis Lee Bieber wrote:

> unless one is reading from a server
> that interprets X-no-archive to mean "delete before reading".

Can't be too careful with security. Destroy it,
memorize it and then read it!

--
Greg

### Aahz

Jun 21, 2009, 11:28:40 PM6/21/09
to
In article <m21vpddb...@cs.uu.nl>,

Piet van Oostrum <pi...@cs.uu.nl> wrote:
>
>I notice that I see several postings on news:comp.lang.python that are
>replies to other postings that I don't see.

As stated previously, my suspicion is that at least some is caused by a
problem with MIME messages and the mail->news gateway on python.org.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha

### Ross Ridge

Jun 22, 2009, 9:36:52 AM6/22/09
to
Piet van Oostrum <pi...@cs.uu.nl> wrote:
>I notice that I see several postings on news:comp.lang.python that are
>replies to other postings that I don't see.

Aahz <aa...@pythoncraft.com> wrote:
>As stated previously, my suspicion is that at least some is caused by a
>problem with MIME messages and the mail->news gateway on python.org.

I'm not sure what MIME would have to do with it, but Piet van Oostrum's
problem is almost certainly as result of the python.org mail to news
gateway mangling the References header. The missing postings he's looking
for don't actually exist. Just go up the thread one more posting and
you'll find the message that was being replied to.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rri...@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //

### Aahz

Jun 23, 2009, 11:13:37 PM6/23/09
to
In article <h1o1dk\$hmc\$1...@rumours.uwaterloo.ca>,
Ross Ridge <rri...@csclub.uwaterloo.ca> wrote:

>Aahz <aa...@pythoncraft.com> wrote:
>>Piet van Oostrum <pi...@cs.uu.nl> wrote:
>>>
>>>>I notice that I see several postings on news:comp.lang.python that are
>>>replies to other postings that I don't see.
>>
>>As stated previously, my suspicion is that at least some is caused by a
>>problem with MIME messages and the mail->news gateway on python.org.
>
>I'm not sure what MIME would have to do with it, but Piet van Oostrum's
>problem is almost certainly as result of the python.org mail to news
>gateway mangling the References header. The missing postings he's looking
>for don't actually exist. Just go up the thread one more posting and
>you'll find the message that was being replied to.

While that's also a bug in Mailman (I have a long-standing to-do item to
fix that), there are also plenty of posts that simply aren't showing up
in c.l.py. As I said, I'm pretty sure (based on what was happening with
c.l.py.announce) that it's some kind of weird problem with the mail->news
gateway with MIME posts.

### Hendrik van Rooyen

Jun 24, 2009, 4:08:36 AM6/24/09
to Aahz, pytho...@python.org
"Aahz" <aa..craft.com> wrote:

> While that's also a bug in Mailman (I have a long-standing to-do item to
> fix that), there are also plenty of posts that simply aren't showing up
> in c.l.py. As I said, I'm pretty sure (based on what was happening with
> c.l.py.announce) that it's some kind of weird problem with the mail->news
> gateway with MIME posts.

I have lately had some posts returned with a "seems to be forged" message.
Two reasons for that:
- first is if I use the smtp server from our local telco - saix - then there is
no apparent relationship between where the message comes from and
where it comes from, if you follow my Irish...
- Second is if the same telco assigns me an IP that has been put on a list

So it is kind of pot luck if you see this message or not.

- Hendrik

### Ross Ridge

Jun 24, 2009, 9:33:48 AM6/24/09
to
Ross Ridge <rri...@csclub.uwaterloo.ca> wrote:
>I'm not sure what MIME would have to do with it, but Piet van Oostrum's
>problem is almost certainly as result of the python.org mail to news
>gateway mangling the References header. The missing postings he's looking
>for don't actually exist. Just go up the thread one more posting and
>you'll find the message that was being replied to.

In article <h1s5l1\$79d\$1...@panix3.panix.com>, Aahz <aa...@pythoncraft.com> wrote:
>While that's also a bug in Mailman (I have a long-standing to-do item to
>fix that), there are also plenty of posts that simply aren't showing up
>in c.l.py.

Well, the message IDs that Piet van Oostrum gave are symptomatic of
the References header bug and and just like he described, my newsreader
also shows Dennis Lee Bieber always replying to posts that don't exist.
Other messages might be getting eaten by the gateway, but the missing
posts that Piet is complaining about almost certainly never existed.

### Aahz

Jun 25, 2009, 7:53:04 PM6/25/09
to
In article <mailman.2045.1245831...@python.org>,

Hendrik van Rooyen <ma...@microcorp.co.za> wrote:
>
>I have lately had some posts returned with a "seems to be forged" message.
>Two reasons for that:
>- first is if I use the smtp server from our local telco - saix - then there is
> no apparent relationship between where the message comes from and
> where it comes from, if you follow my Irish...
>- Second is if the same telco assigns me an IP that has been put on a list