random / lists

Discussion in 'Python' started by Malcolm Clift, Aug 14, 2004.

  1. Hi All,

    I have this so far.


    import random

    things = xrange(int(raw_input("choose no of things (1-8)? ")))

    state = [None,None]

    l = []

    for x in things:
    tmp = random.choice('abcd')
    print state
    while tmp in state[0:2]:
    tmp = random.choice('abcd')
    print "choice ",x+1," is ", tmp
    l.append(tmp)

    state[x%2] = tmp

    print 1

    Could someone help in showing me how to control it like this? That if 'a'
    is choosen it can only be followed by say b or d, if 'd' is choosen it can
    only be followed by c or b etc... I can see how to do this after the list
    has been generated with if statements, but that is impractical. As it is
    at the moment it stop two of the same letters being given one after
    another or the same letter occurring with only one space interviening.

    Thanks,

    Calvin
    Malcolm Clift, Aug 14, 2004
    #1
    1. Advertising

  2. Malcolm Clift

    wes weston Guest

    Malcolm Clift wrote:
    > Hi All,
    >
    > I have this so far.
    >
    >
    > import random
    >
    > things = xrange(int(raw_input("choose no of things (1-8)? ")))
    >
    > state = [None,None]
    >
    > l = []
    >
    > for x in things:
    > tmp = random.choice('abcd')
    > print state
    > while tmp in state[0:2]:
    > tmp = random.choice('abcd')
    > print "choice ",x+1," is ", tmp
    > l.append(tmp)
    >
    > state[x%2] = tmp
    >
    > print 1
    >
    > Could someone help in showing me how to control it like this? That if 'a'
    > is choosen it can only be followed by say b or d, if 'd' is choosen it can
    > only be followed by c or b etc... I can see how to do this after the list
    > has been generated with if statements, but that is impractical. As it is
    > at the moment it stop two of the same letters being given one after
    > another or the same letter occurring with only one space interviening.
    >
    > Thanks,
    >
    > Calvin
    >
    >


    Calvin,
    Could you give a clear statement of what you want to
    do. No code; just a description of what you want to do.
    wes
    wes weston, Aug 15, 2004
    #2
    1. Advertising

  3. Malcolm Clift

    M. Clift Guest

    Hi Wes,

    Thanks for responding.

    What I want is a system that controls the direction in a generated list. If
    the user selects a number a letter is choosen for each number from at random
    from an array. That's fine and easy to do. The hard part is, is that no
    letter can be repeated with only one letter interveining and the letters can
    only follow each other under certain rules.

    These being :
    1. 'a' can be followed by any letter
    2. 'b' can only be followed by 'c'
    3. 'c' can be followed by 'a' or 'd'
    4 'd' can be followed by 'a' or 'c'

    Really it doesn't matter what these rules are for the time being it's just
    working out a way to control the outcome of a list of unknown size. I've
    experimented with lists of say up to 10 long using ' if ' statements for
    each number and different array depending upon the previous letter, but as
    you can imagine it is no good for larger lists as the code just goes on and
    on : ). Rather than I have done before, whereby I said if letter 3 == 'a'
    then letter 4 = choice(bcd) is it possible to say if previous letter equals
    whatever then this letter equals whatever?

    Wes, if you recieved an email direct to you address I'm sorry. I must
    be tierd, as for some reaon I've been hitting the reply button all night
    instead of the reply to group!

    Thanks
    M. Clift, Aug 16, 2004
    #3
  4. Malcolm Clift

    wes weston Guest

    M. Clift wrote:
    > Hi Wes,
    >
    > Thanks for responding.
    >
    > What I want is a system that controls the direction in a generated list. If
    > the user selects a number a letter is choosen for each number from at random
    > from an array. That's fine and easy to do. The hard part is, is that no
    > letter can be repeated with only one letter interveining and the letters can
    > only follow each other under certain rules.
    >
    > These being :
    > 1. 'a' can be followed by any letter
    > 2. 'b' can only be followed by 'c'
    > 3. 'c' can be followed by 'a' or 'd'
    > 4 'd' can be followed by 'a' or 'c'
    >
    > Really it doesn't matter what these rules are for the time being it's just
    > working out a way to control the outcome of a list of unknown size. I've
    > experimented with lists of say up to 10 long using ' if ' statements for
    > each number and different array depending upon the previous letter, but as
    > you can imagine it is no good for larger lists as the code just goes on and
    > on : ). Rather than I have done before, whereby I said if letter 3 == 'a'
    > then letter 4 = choice(bcd) is it possible to say if previous letter equals
    > whatever then this letter equals whatever?
    >
    > Wes, if you recieved an email direct to you address I'm sorry. I must
    > be tierd, as for some reaon I've been hitting the reply button all night
    > instead of the reply to group!
    >
    > Thanks
    >
    >


    Clift,
    I think in your loop you need to have a variable, call it previousLetter,
    to test the latest letter against.

    previousLetter = None
    for x in range(someNum):
    newLetter = someSelection()
    if not previousLetter or someTestOk( previousLetter, newLetter ):
    doYourStuff()
    previousLetter = newLetter

    You might need this in your test:

    >>> ord("c")

    99

    to test if a character follows and by how much.

    wes
    wes
    wes weston, Aug 16, 2004
    #4
  5. Malcolm Clift

    Paul Rubin Guest

    "M. Clift" <> writes:
    > What I want is a system that controls the direction in a generated list. If
    > the user selects a number a letter is choosen for each number from at random
    > from an array. That's fine and easy to do. The hard part is, is that no
    > letter can be repeated with only one letter interveining and the letters can
    > only follow each other under certain rules.


    Is this a homework assignment?
    Paul Rubin, Aug 16, 2004
    #5
  6. Malcolm Clift

    M. Clift Guest

    Hi Wes,

    Thankyou. I'll now go away and experiment with how to use it as I've just
    started with python, but I should be able to learn a lot with this. It would
    seem to be exactly what I was after. : )

    Malcolm
    M. Clift, Aug 16, 2004
    #6
  7. Malcolm Clift

    M. Clift Guest

    Hi Paul,

    No, not homework, I'm a bit old for that : ) It's an idea for a simple
    decision maker.

    Malcolm
    M. Clift, Aug 16, 2004
    #7
  8. Malcolm Clift

    Russell Blau Guest

    "M. Clift" <> wrote in message
    news:cformj$vso$...
    > Hi Wes,
    >
    > Thanks for responding.
    >
    > What I want is a system that controls the direction in a generated list.

    If
    > the user selects a number a letter is choosen for each number from at

    random
    > from an array. That's fine and easy to do. The hard part is, is that no
    > letter can be repeated with only one letter interveining and the letters

    can
    > only follow each other under certain rules.
    >
    > These being :
    > 1. 'a' can be followed by any letter
    > 2. 'b' can only be followed by 'c'
    > 3. 'c' can be followed by 'a' or 'd'
    > 4 'd' can be followed by 'a' or 'c'
    >
    > Really it doesn't matter what these rules are for the time being it's just
    > working out a way to control the outcome of a list of unknown size.


    How about if you define a dictionary mapping each letter to a list of the
    letters that are allowed to follow it, then use random.choice() to pick one
    member of the list. For example,

    mapping = {'a': ['b', 'c', 'd'], 'b': ['c'], 'c': ['a', 'd'],
    'd': ['a', 'c']}

    then after you added the last letter to l, you could just do

    allowed = mapping[l[-1]][:] # nneds to be a copy because it may get
    modified
    if l[-2] in allowed: # no letter can appear two out of three
    places
    allowed.remove[l[-2]]
    l.append(random.choice(allowed)

    Of course, this doesn't account for every case (like, it won't work if l is
    the empty list), and it may not satisfy all your desired rules, but it's a
    start.


    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.
    Russell Blau, Aug 16, 2004
    #8
  9. Malcolm Clift

    M. Clift Guest

    Hi Russell,

    That looks really good. It would seem to allow for a lot of control. Yet
    another example for me to learn from! As someone new to programming, I'm
    only just starting to realise that in code there is no one answer to a
    problem.

    Thanks,

    Malcolm
    M. Clift, Aug 16, 2004
    #9
    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. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    439
    mikeb
    Jun 24, 2004
  2. Maziar Aflatoun

    Random not really random...

    Maziar Aflatoun, Aug 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    26,651
    Maziar Aflatoun
    Aug 5, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    380
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. globalrev
    Replies:
    4
    Views:
    734
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,099
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page