Python Forum
Generate unique random numbers from different lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate unique random numbers from different lists
#6
As an example, here's your for loop redone:

while len(selection_list) < 5:
    # Note that if you are just selecting one item, random.choice returns the item, not a list
    i = random.choice(whole_list)
 
    # We no longer need to check status, just outcasts and what's already been selected.
    if (i not in outcast_list) and (i not in selection_list):
        selection_list.append(i)
 
    # We no longer need to check for a break, because the while loop will do that
Right after every append, it goes back to the while statement. The while statement checks the current length. If we don't have five items yet, it does the loop again. If we have five or more, it skips to the end of the loop and continues on with the rest of the program. The loop is directly testing the termination condition, rather than getting it through the proxy of status.

That works here, because the termination condition is simple. If you have to do some processing to get the condition, so you use a conditional with a break, like you did:

while True:
    choice = input('Please select a menu item: ')
    if is_valid(choice):
        break
    print('That is not a valid selection.')
Here, we need to get the user input, and also check it for validity, before we can be sure it's time to exit the loop. But again, we don't need the proxy of status, we just check the validity of the choice, and then exit the loop if the choice is valid.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Generate unique random numbers from different lists - by ichabod801 - May-24-2019, 07:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  random numbers, randint janeik 2 577 Nov-27-2023, 05:17 PM
Last Post: janeik
  Sample random, unique string pairs from a list without repetitions walterwhite 1 465 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,260 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 783 Feb-28-2023, 10:55 PM
Last Post: deanhystad
  List of random numbers astral_travel 17 2,725 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,531 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Generate random id (please help!) Kjaglewicz 8 1,990 Aug-29-2022, 09:37 AM
Last Post: fracjackmac
  generating random string unique forever Skaperen 5 2,340 Aug-20-2021, 07:15 AM
Last Post: Gribouillis
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,834 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Generate random hex number ZYSIA 1 11,636 Jul-16-2021, 09:21 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020