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
#5
(May-24-2019, 05:58 PM)ichabod801 Wrote: Some tips on your code: since everything in whole_list is an int, you don't need to convert them to an in on line 16. Also, you are not really using status. You break out of the while loop, so it doesn't end because you change status to True. And when you check status on line 19 it is necessarily False. The only time it is True is right before you break out of the loop that line 19 is in. I would change line 13 to while len(selection_list) < 5:. Then you can remove lines 22-25 and remove status from line 19.

Here's a not-so-short form of my solution, if it helps:

valid = []
for number in whole_list:
    if number not in outcast_list:
        valid.append(number)
selection_list = random.sample(valid, 5)

Thanks for the tips. It actually took me a while to understand why I shouldn't use boolean (i.e. status) to check the validity. Just to understand more on using boolean to check for status, can you please share with me more by quoting an example (with code snippet)?
Reply


Messages In This Thread
RE: Generate unique random numbers from different lists - by Takeshio - May-24-2019, 06:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  random numbers, randint janeik 2 647 Nov-27-2023, 05:17 PM
Last Post: janeik
  Sample random, unique string pairs from a list without repetitions walterwhite 1 553 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,566 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 831 Feb-28-2023, 10:55 PM
Last Post: deanhystad
  List of random numbers astral_travel 17 2,903 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,668 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Generate random id (please help!) Kjaglewicz 8 2,142 Aug-29-2022, 09:37 AM
Last Post: fracjackmac
  generating random string unique forever Skaperen 5 2,438 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,932 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Generate random hex number ZYSIA 1 12,019 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