Hey there! Sign in to join this conversationNew here? Join for free
x Turn on thread page Beta

How do you generate random numbers but with no duplicates? Python watch

    • Thread Starter
    Offline

    1
    ReputationRep:
    Hi,

    I need some help with a school assignment where we have to create a program that chooses a random pair from a list of words, and outputs it to the user, then print one word of another pair, and has the user guess what its match is. For example, the computer randomly chooses the pair "Happy" & "Sad", then prints "Hot", and asks the user to input the match, which in this case would be "Cold". I have already successfully created the program however, there are duplicates in the pairs that are generated, and I need help preventing these duplicates because they are assessments. Any ideas?
    Offline

    3
    ReputationRep:
    Run the random function on each iteration of a loop on a list of indexes or words (don't know how you did it before).
    After they are chosen, remove them from the list.

    Posted from TSR Mobile
    Offline

    3
    ReputationRep:
    You're probably using a list to store your data, so you could use remove() or pop().

    https://docs.python.org/2/tutorial/d...#more-on-lists
    • Thread Starter
    Offline

    1
    ReputationRep:
    (Original post by Push_More_Button)
    You're probably using a list to store your data, so you could use remove() or pop().

    https://docs.python.org/2/tutorial/d...#more-on-lists
    When I used remove() & pop(), I end up later in the code getting an error saying that the index of the random numbers generated are out of range. If you wish to see the code to get a better understanding, visit this page:

    http://stackoverflow.com/questions/3...-or-duplicated

    Please, I need this for Today!
    Offline

    16
    ReputationRep:
    You are generating a random number using a hard coded '16'. Don't hard-code numbers (or literals), either use a constant or a variable or a function. That is your undoing.

    In this case, it is to pick an entry from the list, and the length of the list is changing. So the '16' needs to be replaced with something that represents the length of the list - either a variable that counts down or (less efficient but more correct) a count of items in the list.
    • Thread Starter
    Offline

    1
    ReputationRep:
    (Original post by Simes)
    You are generating a random number using a hard coded '16'. Don't hard-code numbers (or literals), either use a constant or a variable or a function. That is your undoing.

    In this case, it is to pick an entry from the list, and the length of the list is changing. So the '16' needs to be replaced with something that represents the length of the list - either a variable that counts down or (less efficient but more correct) a count of items in the list.
    I've decided to go with an idea where an incremented decreasing variable of the length of the list is used to generate a random integer, so that way the index doesn't go out of range. However I have approached a new problem when I attempt to execute a function I created which appends a variable (who's value is that of the list) to another list.

    Traceback (most recent call last): File "C:\Users\Timic\Downloads\Py thon Opposites Prorgam (1).py", line 47, in <module> tracker()TypeError: 'str' object is not callable

    The function is the following:

    def tracker():
    global tracker
    global trackerresult
    trackerresult = []
    tracker = opposite1[decider]
    trackerresult.append(tracker)
    print(trackerresult)

    Why is it doing this?
    • Thread Starter
    Offline

    1
    ReputationRep:
    [QUOTE=Simes;60097777]You are generating a random number using a hard coded '16'. Don't hard-code numbers (or literals), either use a constant or a variable or a function. That is your undoing.

    I've decided to go with an idea where an incremented decreasing variable of the length of the list is used to generate a random integer, so that way the index doesn't go out of range. However I have approached a new problem when I attempt to execute a function I created which appends a variable (who's value is that of the list) to another list.

    Traceback (most recent call last): File "C:\Users\Timic\Downloads\Py thon Opposites Prorgam (1).py", line 47, in <module> tracker()TypeError: 'str' object is not callable

    The function is the following:

    def tracker():
    global tracker
    global trackerresult
    trackerresult = []
    tracker = opposite1[decider]
    trackerresult.append(tracker)
    print(trackerresult)

    Why is it doing this?
    Offline

    3
    ReputationRep:
    As Simes pointed out, you're always getting a number between 0 and 16, but when you remove an element from either of your lists you are making it shorter so if you remove several elements and then randomly pick 16 it will not being able to find it (as there is no 16th element) and will throw an exception.

    A better method for doing this would be to use a list of tuples instead of two lists, this means you can store both of your words in the same element of the list, present the first one to the user and then have the second one to compare against.

    PS - Please don't use global variables, they're yucky.
    Offline

    14
    ReputationRep:
    define the pairs, a map or a 2d array would be most suitable.
    add another 2d array that will store the picked pairs.
    randomly lookup a pair using any random algorithm (math.random for instance) then check if the generated pair is not contained in the other 2d array to ensure it isn't a duplicate.
 
 
 
Reply
Submit reply
Turn on thread page Beta
Updated: October 25, 2015
Poll
Do you agree with the proposed ban on plastic straws and cotton buds?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.