The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Python function comparison
Discuss Python function comparison in the Python Programming forum on Dev Shed. Python function comparison Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 13th, 2012, 09:51 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 6
Time spent in forums: 1 h 33 m 14 sec
Reputation Power: 0
|
|
|
Python function comparison
I'm just learning Python. I want to use:
while random.random() < .5 ....
to iterate until I get a random number > .5, but no joy.
It seems like it ought to work, since random.random() < .5 in most contexts returns a boolean value.
Thanks for any help.
|

September 14th, 2012, 03:16 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by casey2u I'm just learning Python. I want to use:
while random.random() < .5 ....
to iterate until I get a random number > .5, but no joy.
It seems like it ought to work, since random.random() < .5 in most contexts returns a boolean value. |
It ought to work and as far as I can see it DOES work unless you’re doing something you don’t mention here. See this sample session from PyShell:
Code:
for i in range(10):
while random.random() < 0.5:
print('Round {0} -- Not yet, stay tuned'.format(i))
Round 1 -- Not yet, stay tuned
Round 1 -- Not yet, stay tuned
Round 2 -- Not yet, stay tuned
Round 3 -- Not yet, stay tuned
Round 3 -- Not yet, stay tuned
Round 5 -- Not yet, stay tuned
Round 6 -- Not yet, stay tuned
Round 7 -- Not yet, stay tuned
Round 8 -- Not yet, stay tuned
Round 8 -- Not yet, stay tuned
Round 8 -- Not yet, stay tuned
Round 9 -- Not yet, stay tuned
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

September 14th, 2012, 04:33 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Your idea isn't usable exactly as you stated it, but you can do something like
python Code:
Original
- python Code |
|
|
|
x = 0 while x < 0.5: x = random.random()
However, an easier way to get a random number in the range [0.5, 1) is (1 + random.random())/2.
|

September 14th, 2012, 05:40 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by Lux Perpetua Your idea isn't usable exactly as you stated it, |
Not true, unless the actual random value is needed. See:
Code:
>>> import random
>>> while random.random() < 0.8:
print('Wait...')
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
>>> while random.random() < 0.8:
print('Wait...')
>>> while random.random() < 0.8:
print('Wait...')
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
Wait...
|

September 14th, 2012, 11:34 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 6
Time spent in forums: 1 h 33 m 14 sec
Reputation Power: 0
|
|
|
Super Oscar, you've got exactly the idea: I want to iterate until I reach a number, randomly generated, greater than .5 (or whatever). It's like asking how many times do I flip a coin until I get a head.
Anyway, I just tried EXACTLY your code in 2.7.3 and it doesn't work, in either command line or IDE. Note that in 2.7.3, after I import random, I can print random.random() and it works.
I then installed 3.2.3, and after I import random I CAN'T print random.random(). Nor, of course, does your code work.
Sooo: what am I doing wrong? At the very least I'd expect random to work the same in both releases. Can other people run the code you posted? It seems like a reasonably efficient code sequence to create a random-length iteration based on a given probability, but I'd be open to fine-tunings.
Thanks!
Also note: I'm a rank beginner, and just installed Python. So I haven't flipped any switches or done any set up or anything... make no assumptions!
|

September 14th, 2012, 11:45 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 6
Time spent in forums: 1 h 33 m 14 sec
Reputation Power: 0
|
|
|
I think I notice that a different random module is imported in 2.7.3 and 3.2.3. But I can't quite tell yet. It seems so based on help. I'm just too clueless to be certain, yet
|

September 15th, 2012, 12:52 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | Originally Posted by SuperOscar Not true, unless the actual random value is needed. | That's what I meant, actually. Sorry, I could have been clearer. Quote: | Originally Posted by casey2u Anyway, I just tried EXACTLY your code in 2.7.3 and it doesn't work, in either command line or IDE. Note that in 2.7.3, after I import random, I can print random.random() and it works.
I then installed 3.2.3, and after I import random I CAN'T print random.random(). Nor, of course, does your code work. | You didn't provide enough information. How about an error message or two? SuperOscar's code is actually compatible with both Python 2.x and Python 3.x (though it's not idiomatic for Python 2, unless you do
Code:
from __future__ import print_function
at the top of your code).
Let me try to be helpful anyway: - You have to import random at the top of SuperOscar's code. Did you forget?
- Regarding Python 3, I'm just guessing (since you didn't post the code you tried) that you tried to use the print statement and not the print function. If you don't know the difference, stick to Python 2.x for now and read up on the differences between Python 2 and Python 3 when you get your stuff working on Python 2.
Quote: | Originally Posted by casey2u
Sooo: what am I doing wrong? At the very least I'd expect random to work the same in both releases. | Be careful: Python 3 and Python 2 are not compatible! Python 3 is an evolution of the language, and the developers deliberately broke compatibility with Python 2 in order to make the language better. Quote: | Originally Posted by casey2u Can other people run the code you posted? | Yes, with the proviso I mentioned above in point 1. Quote: | Originally Posted by casey2u It seems like a reasonably efficient code sequence to create a random-length iteration based on a given probability, but I'd be open to fine-tunings. | Okay, now I see what you're trying to do; I misunderstood it in my previous reply. Your solution to this is fine unless you have some specific goal in mind like minimizing the amount of randomness you consume (which probably sounds strange, but it's an interesting thing to think about).
|

September 15th, 2012, 02:15 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 6
Time spent in forums: 1 h 33 m 14 sec
Reputation Power: 0
|
|
|
For simplicity, I'll stick with Python 2.
I'm not sure how to copy code segments/quotes here, or copy something out of the command line window.
When I type
print random.random()
I get (for example)
0.1624687 etc.
When I type
while random.random() < 0.5:
I get
File "<stdin>, line 1
and then the previous "while" statement with a pointer at the .5, and
SyntaxError: invalid syntax
Sorry I didn't include error messages before: they don't seem very helpful, to me.
Thanks for your help.
If you can point me to anything about how to quote code segments & error messages, that would be great.
|

September 15th, 2012, 02:39 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 6
Time spent in forums: 1 h 33 m 14 sec
Reputation Power: 0
|
|
|
SuperOscar: I just tried to install pyshell, but it's beyond me I guess, for now. I've downloaded it, I click on the .bat (which some instructions say), and nothing happens. Windows 7. I also tried clicking on the pyshell.py from another install. In both cases a window opens very briefly, I think it's the python command line, and then closes. I don't know anything about shells or running scripts yet.
In terms of my code: I installed python, imported random, and coded what I included here. Nothing else. I can't understand how it could run for you and not for me.
|

September 15th, 2012, 03:17 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by casey2u SuperOscar: I just tried to install pyshell, but it's beyond me I guess, for now. |
That’s all right, PyShell is just an IDLE-like environment where you can paste and run code snippets quickly; it’s got nothing to do with your actual problem.
I must admit I’m at a loss. I’ve tried the code in both Python 2.7.3 and 3.2.3 and it works. “while random.random() < 0.5:” most definitely does not contain a syntax error.
The only thing I can think of is that the syntax error occurs BEFORE that line. Maybe you could copy the whole error message rigmarole and paste it here?
|

September 15th, 2012, 04:02 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | Originally Posted by casey2u For simplicity, I'll stick with Python 2.
I'm not sure how to copy code segments/quotes here, or copy something out of the command line window.
When I type
print random.random()
I get (for example)
0.1624687 etc.
When I type
while random.random() < 0.5:
I get
File "<stdin>, line 1
and then the previous "while" statement with a pointer at the .5, and
SyntaxError: invalid syntax | No no no. Copy/paste the code into a text file, say, test.py:
python Code:
Original
- python Code |
|
|
|
import random for i in range(10): while random.random() < 0.5: print('Round {0} -- Not yet, stay tuned'.format(i))
Then open a command prompt window, navigate to the folder where you saved the file, and enter:
Since you have both Python 2 and Python 3, you can also try python2 test.py and python3 test.py to ensure the use of one or the other. If it doesn't work, post the error message verbatim.
I'm sorry I can't give you more specific instructions, since I don't use Windows 7 (or Windows generally). If I recall, on Windows XP, you get the command prompt by going to "Run" on the "Start" menu and typing "cmd".
To post code on this forum, you need to use the [code][/code] or [highlight][/highlight] tags. To see how it's done, click "Reply" on the bottom of any post that contains code. You'll be able to see the exact code that person used to make that post.
|

September 15th, 2012, 08:04 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 6
Time spent in forums: 1 h 33 m 14 sec
Reputation Power: 0
|
|
|
Thanks for your help. I got it working, and I still don't understand what changed: maybe I left off the colon or something. I'd swear I'm typing in the same thing I started with. It's easy to miss the obvious when you don't really know anything...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|