# [Patches] [ python-Patches-1191489 ] Simplify logic in random.py

SourceForge.net noreply at sourceforge.net
Sat Apr 30 07:07:53 CEST 2005

```Patches item #1191489, was opened at 2005-04-28 01:31
Message generated for change (Comment added) made by tim_one
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1191489&group_id=5470

Category: Library (Lib)
Group: Python 2.5
Status: Open
>Resolution: Accepted
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
>Assigned to: Raymond Hettinger (rhettinger)
Summary: Simplify logic in random.py

Initial Comment:
Simplify the logic in several methods for better
readability, maintainability, and speed:

* while True --> while 1
* apply de'morgan's theorem to simplify compound
conditionals
* factor out repeated test:  (p>1.0) and (p <= 1.0)
* replace pow() with **

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

>Comment By: Tim Peters (tim_one)
Date: 2005-04-30 01:07

Message:
Logged In: YES
user_id=31435

Raymond, I checked the semantics, and aver that all the
transformations here are semantically correct.  Marked as
Accepted -- go for it.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-04-29 02:12

Message:
Logged In: YES
user_id=80475

Thanks for looking at the patch.  What I really need is for
a second person to verify that the transformations are
semantically neutral (no change in overall logic).

FWIW, I was the one who did rev 1.39.  It turns out that it
was a premature modernization that cost some performance.
Only the while 1 form is optimized by CPython, Jython, and
Pysco.

Have to disagree with you on pow(). The operator form runs
faster and corresponds more closely to the way formulas are
written in math textbooks and in other languages (except C
which doesn't have a pow operator).  This is the reason we

If you can a chance, please look closely at the logic
transformations to make sure I did them accurately.  Thanks
again.

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

Comment By: Michiel de Hoon (mdehoon)
Date: 2005-04-29 01:42

Message:
Logged In: YES
user_id=488897

* I don't see how "while 1" is more readable than "while
True". Note also that in older Python versions, this
actually was "while 1"; it was changed to "while True" in
revision 1.39. I can't recommend changing it back.

* This pertains to line 424. I agree that the line in the
patch is more readable and perhaps faster (though marginally
so).

* I agree with you that there is a repeated test in the
current code. In the patch, I would stick with pow() instead
of ** (see my next comment)

* Unnecessary change. Some people like pow(), others like
**. I see no reason to change.

General comment: It is usually better to have different
changes in separate patches. True, the changes in the patch
all apply to random.py, but otherwise they don't have much
in common.

(I'm just a patch reviewer, not an official Python
developer, so you don't need to take my comments too seriously.)

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

You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1191489&group_id=5470
```