Another newbie question from Nathan.

Discussion in 'Python' started by Nathan Pinno, Jul 2, 2005.

  1. Nathan Pinno

    Nathan Pinno Guest

    Hi all.

    How do I make the computer generate 4 random numbers for the guess? I want
    to know because I'm writing a computer program in Python like the game
    MasterMind.

    Thanks.

    --
    Nathan Pinno
    http://www.npinnowebsite.ca/



    --


    ----------------------------------------------------------------
    Posted via UsenetRevolution.com - Revolutionary Usenet
    ** HIGH RETENTION ** Specializing in Large Binaries Downloads **
    http://www.UsenetRevolution.com
     
    Nathan Pinno, Jul 2, 2005
    #1
    1. Advertising

  2. Nathan Pinno said unto the world upon 02/07/2005 02:25:
    >
    > Hi all.
    >
    > How do I make the computer generate 4 random numbers for the guess? I want
    > to know because I'm writing a computer program in Python like the game
    > MasterMind.
    >
    > Thanks.
    >
    > --
    > Nathan Pinno
    > http://www.npinnowebsite.ca/


    Hi Nathan,

    do you know how to make a program doing anything 4 times? If so, do
    that :)

    Here's one way:

    >>> import random
    >>> def get_master_mind_guess():

    .... guess = []
    .... for i in range(4):
    .... guess.append(random.random())
    .... return guess
    ....
    >>> get_master_mind_guess()

    [0.21236215888513332, 0.096166729147619479, 0.037667620585694728,
    0.77529759474635485]
    >>>


    I doubt you need numbers between 0 and 1, but adjust to suit.

    I'd also suggest that you might find the Tutor list of real help.
    <http://mail.python.org/mailman/listinfo/tutor>. Nothing wrong with
    not knowing how to do stuff like this -- few were born knowing, after
    all -- but the Tutor list specializes in answering in a way targeted
    to beginners. It's where I learned much of what I know.

    Best,

    Brian vdB
     
    Brian van den Broek, Jul 2, 2005
    #2
    1. Advertising

  3. On Sat, 02 Jul 2005 00:25:00 -0600, Nathan Pinno wrote:

    >
    >
    > Hi all.
    >
    > How do I make the computer generate 4 random numbers for the guess? I want
    > to know because I'm writing a computer program in Python like the game
    > MasterMind.


    First you get the computer to generate one random number. Then you do it
    again three more times.

    If you only need to do it once, you could do it this way:

    import random # you need this at the top of your program
    x0 = random.random()
    x1 = random.random()
    x2 = random.random()
    x3 = random.random()


    But if you need to do it more than once, best to create a function that
    returns four random numbers in one go.

    def four_random():
    """Returns a list of four random numbers."""
    L = [] # start with an empty list
    for i in range(4):
    L.append(random.random())
    return L

    and use it this way:

    rand_nums = four_random()
    # rand_nums is a list of four numbers
    print rand_nums[0] # prints the first random number
    print rand_nums[3] # prints the last one

    or like this:

    alpha, beta, gamma, delta = four_random()
    # four names for four separate numbers


    --
    Steven.
     
    Steven D'Aprano, Jul 3, 2005
    #3
  4. Nathan Pinno

    Nathan Pinno Guest

    Hi all,

    How do I make Python get a def? Is it the "get" function, or something
    else? I need to know so that I can get a def for that computer
    MasterMind(tm) game that I'm writing.

    BTW, I took your advice, and wrote some definitions for my Giant
    Calculator program. Might make the code easier to read, but harder to code
    because I have to keep going to the top to read the menu. Not that fun, but
    necessary for a smooth program, I guess.

    Nathan Pinno

    "Steven D'Aprano" <> wrote in message
    news:p...
    > On Sat, 02 Jul 2005 00:25:00 -0600, Nathan Pinno wrote:
    >> Hi all.
    >> How do I make the computer generate 4 random numbers for the guess? I

    want
    >> to know because I'm writing a computer program in Python like the game
    >> MasterMind.

    > First you get the computer to generate one random number. Then you do it
    > again three more times.
    > If you only need to do it once, you could do it this way:
    > import random # you need this at the top of your program
    > x0 = random.random()
    > x1 = random.random()
    > x2 = random.random()
    > x3 = random.random()
    > But if you need to do it more than once, best to create a function that
    > returns four random numbers in one go.
    > def four_random():
    > """Returns a list of four random numbers."""
    > L = [] # start with an empty list
    > for i in range(4):
    > L.append(random.random())
    > return L
    > and use it this way:
    > rand_nums = four_random()
    > # rand_nums is a list of four numbers
    > print rand_nums[0] # prints the first random number
    > print rand_nums[3] # prints the last one
    > or like this:
    > alpha, beta, gamma, delta = four_random()
    > # four names for four separate numbers
    > Steven.
    > http://mail.python.org/mailman/listinfo/python-list





    --


    ----------------------------------------------------------------
    Posted via UsenetRevolution.com - Revolutionary Usenet
    ** HIGH RETENTION ** Specializing in Large Binaries Downloads **
    http://www.UsenetRevolution.com
     
    Nathan Pinno, Jul 9, 2005
    #4
  5. Nathan Pinno

    Guest

    Nathan Pinno wrote:
    > Hi all,
    >
    > How do I make Python get a def? Is it the "get" function, or something
    > else? I need to know so that I can get a def for that computer
    > MasterMind(tm) game that I'm writing.
    >
    > BTW, I took your advice, and wrote some definitions for my Giant
    > Calculator program. Might make the code easier to read, but harder to code
    > because I have to keep going to the top to read the menu. Not that fun, but
    > necessary for a smooth program, I guess.
    >
    > Nathan Pinno


    Read the following tutorials first, perhaps in that order.

    http://honors.montana.edu/~jjc/easytut/easytut/
    http://www.ibiblio.org/obp/thinkCSpy/
    http://docs.python.org/tut/tut.html
     
    , Jul 10, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    2
    Views:
    464
    Ralf Hildebrandt
    Sep 1, 2003
  2. Dave Newberry
    Replies:
    0
    Views:
    666
    Dave Newberry
    Oct 17, 2004
  3. SStory
    Replies:
    1
    Views:
    295
    Kevin Spencer
    Aug 4, 2003
  4. Rob R. Ainscough
    Replies:
    3
    Views:
    353
    Rob R. Ainscough
    Jun 14, 2005
  5. Jerry C.
    Replies:
    8
    Views:
    263
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page