Python Forum
Generate a list of numbers within a list of arbitrary numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate a list of numbers within a list of arbitrary numbers
#1
Hi Guys,

Suppose I have a list of arbitrary numbers (23, 2, 49, 100, 101) and from this list, I want to randomly pick 3 unique numbers and store them inside a new list. I can’t use random.sample() function, as the function will generate the numbers based on a range of sequence numbers.

Can you please advise me on how to pick 3 unique numbers from a list of arbitrary numbers, and store them in a new list?

Thank you.
Reply
#2
I don't understand what the problem with using random.sample() is
from random import sample

population = (23, 2, 49, 100, 101)
my_sample = sample(population, 3)
print(my_sample)
Output:
[101, 100, 49] >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Nov-07-2018, 12:30 PM)buran Wrote: I don't understand what the problem with using random.sample() is
from random import sample

population = (23, 2, 49, 100, 101)
my_sample = sample(population, 3)
print(my_sample)
Output:
[101, 100, 49] >>>

Thanks Buran. Maybe I misunderstood that random.sample() will need to specify “range” of sequence numbers as its parameter, in order for it to work.

Once again, thank you for your help.
Reply
#4
I think you were just confused by the example with range.
Note that if you have repeated numbers in the original list it's also possible to have repeating elements in the sample
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Nov-07-2018, 02:36 PM)buran Wrote: I think you were just confused by the example with range.
Note that if you have repeated numbers in the original list it's also possible to have repeating elements in the sample

Yup. Understand that repeated numbers may be picked up if there are duplicates in the population list.

Thanks Buran.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using a For Loop to subtract numbers from an input function. Anunderling 9 2,821 Sep-22-2025, 08:56 PM
Last Post: deanhystad
  Complex Numbers Curbie 4 3,326 Feb-18-2025, 09:49 PM
Last Post: bowlofred
  [split] Prime numbers saima 1 1,029 Dec-09-2024, 02:19 AM
Last Post: jefsummers
  creating arbitrary local variable names Skaperen 9 2,991 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  need help writing a program to run VIN numbers, any advice? Jhins007 4 2,045 Aug-23-2024, 08:06 AM
Last Post: DeaD_EyE
  python code to calculate mean of an array of numbers using numpy viren 3 2,126 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Strange behavior list of list mmhmjanssen 3 2,336 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 24,428 Jan-05-2024, 08:30 PM
Last Post: sgrey
  random numbers, randint janeik 2 2,539 Nov-27-2023, 05:17 PM
Last Post: janeik
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 3,508 Sep-24-2023, 05:03 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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