Creating a random integer....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Code_Dark

    Creating a random integer....

    Hi, sorry to bore you with my newbie questions, but I _am_ a newbie
    so.. right. Anyway, I was working out a "number guessing game", which
    works fine, except I wanted to make the number a random number so it's
    different every time... can you please tell me what to do to get
    python to create a random number?

    Thanks,

    - Code Dark
  • Ben Finney

    #2
    Re: Creating a random integer....

    On 6 Nov 2003 21:40:57 -0800, Code_Dark wrote:[color=blue]
    > Hi, sorry to bore you with my newbie questions, but I _am_ a newbie
    > so.. right.[/color]

    Welcome! Start here:

    <http://www.python.org/doc/>
    [color=blue]
    > Anyway, I was working out a "number guessing game", which
    > works fine, except I wanted to make the number a random number so it's
    > different every time... can you please tell me what to do to get
    > python to create a random number?[/color]

    The first place to check for "How do I get python to do foo" is the
    Python library reference, detailing all the modules in the standard
    library:

    <http://www.python.org/doc/lib/>

    A search for "random" on that page will be illuminating.

    --
    \ "I hope that after I die, people will say of me: 'That guy sure |
    `\ owed me a lot of money.'" -- Jack Handey |
    _o__) |
    Ben Finney <http://bignose.squidly .org/>

    Comment

    • Matthew Thorley

      #3
      Re: Creating a random integer....

      Python has a module for doing just that its called random. Try this:

      import random

      # print a random interger between 0 100
      print random.randint( 0,100)

      # print a random number from the list lst
      lst = [1,3,6,17,87,330]
      print random.choice(l st)

      Hope this helps. I recommend picking up a copy of Python Standard
      Library by Fredrick Lundh at your local book store or library. It will
      have more detailed information about the random module and the PSL.

      -matthew

      Code_Dark wrote:[color=blue]
      > Hi, sorry to bore you with my newbie questions, but I _am_ a newbie
      > so.. right. Anyway, I was working out a "number guessing game", which
      > works fine, except I wanted to make the number a random number so it's
      > different every time... can you please tell me what to do to get
      > python to create a random number?
      >
      > Thanks,
      >
      > - Code Dark[/color]

      Comment

      Working...