#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    6
    Rep 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    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 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Your idea isn't usable exactly as you stated it, but you can do something like
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    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...
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    6
    Rep 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!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    6
    Rep 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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    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.
    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.

    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.
    Originally Posted by casey2u
    Can other people run the code you posted?
    Yes, with the proviso I mentioned above in point 1.
    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).
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    6
    Rep 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.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    6
    Rep 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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    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?
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    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:
    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:
    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.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    6
    Rep 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...

IMN logo majestic logo threadwatch logo seochat tools logo