Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 13th, 2012, 09:51 PM
casey2u casey2u is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 6 casey2u User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old September 14th, 2012, 03:16 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 17 m 55 sec
Reputation Power: 65
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)

Reply With Quote
  #3  
Old September 14th, 2012, 04:33 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Your idea isn't usable exactly as you stated it, but you can do something like
python Code:
Original - python Code
  1. x = 0
  2. while x < 0.5:
  3.     x = random.random()
However, an easier way to get a random number in the range [0.5, 1) is (1 + random.random())/2.

Reply With Quote
  #4  
Old September 14th, 2012, 05:40 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 17 m 55 sec
Reputation Power: 65
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...

Reply With Quote
  #5  
Old September 14th, 2012, 11:34 PM
casey2u casey2u is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 6 casey2u User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #6  
Old September 14th, 2012, 11:45 PM
casey2u casey2u is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 6 casey2u User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old September 15th, 2012, 12:52 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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:
  1. You have to import random at the top of SuperOscar's code. Did you forget?
  2. 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).

Reply With Quote
  #8  
Old September 15th, 2012, 02:15 AM
casey2u casey2u is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 6 casey2u User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old September 15th, 2012, 02:39 AM
casey2u casey2u is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 6 casey2u User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old September 15th, 2012, 03:17 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 17 m 55 sec
Reputation Power: 65
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?

Reply With Quote
  #11  
Old September 15th, 2012, 04:02 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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
  1. import random
  2.  
  3. for i in range(10):
  4.     while random.random() < 0.5:
  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:
Code:
python test.py

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.

Reply With Quote
  #12  
Old September 15th, 2012, 08:04 PM
casey2u casey2u is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 6 casey2u User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python function comparison

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap