[Python-bugs-list] [ python-Bugs-506647 ] random.cunifvariate() incorrect?

noreply@sourceforge.net noreply@sourceforge.net
Mon, 20 May 2002 20:27:45 -0700


Bugs item #506647, was opened at 2002-01-21 16:19
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=506647&group_id=5470

Category: Python Library
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Maciej Kalisiak (abcdefghijklmno)
Assigned to: Raymond Hettinger (rhettinger)
Summary: random.cunifvariate() incorrect?

Initial Comment:
random.cunifvariate() currently is defined as:
  return (mean + arc * (self.random() - 0.5)) % _pi

First of all, this returns values in the range [0, pi)
due to the modulus, which really makes it a
semi-circular distribution, not circular (but I'm not a
mathie, so perhaps I'm wrong).

I think the normalizing step ("% _pi", or whatever this
gets changed to) should probably be mentioned in the
documentation for this function; it caught me by
surprise, and I expect it will catch some others too.  

With this defintion, the function returns a random
value in the range [mean-arc/2, mean+arc/2).  The docs
say that arc, like mean, can only be in the range
[0,pi].  This would imply that one cannot get values in
the other half of the circle, i.e. [pi, 2*pi).  Since
there doesn't seem to be anything stopping the caller
from using an arc in [0,2*pi], perhaps the
documentation should be updated?


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2002-05-20 22:27

Message:
Logged In: YES 
user_id=80475

So be it.  

If there are no objections received before Thursday May 
24th, I'll deprecate it.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2002-05-20 22:11

Message:
Logged In: YES 
user_id=31435

I recommend to deprecate this function instead of "fixing" 
it.  If we deprecate it in 2.3, we can lose it entirely for 
2.4.  The function appears useless to me with or without 
the suggested fixes, and anyone who *did* want 
something "like this" would just call random.uniform() 
passing appropriate bounds anyway.  Indeed, Raymond's patch 
amounts to making cunifvariate a synonym for calling 
random.uniform(mean-arc/2, mean+arc/2).  We don't need a 
separate function for something so trivial.

BTW, I've never seen the word "cunifvariate" outside of 
Python's random module.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-05-15 06:00

Message:
Logged In: YES 
user_id=21627

I'm not qualified to comment on the correctness of this
change, so unassigning it. Maybe Tim knows what a
cunifvariate is?

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-05-14 16:53

Message:
Logged In: YES 
user_id=80475

Martin, please approve the patch and I'll load it for Py2.3.

The docs don't match the function and the actual behavior 
is weird.  Thinking in degrees, calling with a mean of 175 
and an arc of 20 returns {x | 0<x<5 or 165<x<180} with an 
expected mean of 130 instead of the specified mean of 175.

Google reports no non-Python examples of cunifvariate so 
there is no standard to match against.

The patch removes the Pi normalization in the 
implementation and removes the superfluous domain 
restriction in the docs.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=506647&group_id=5470