Python Forum
Multiple Random Selections with no dulpicates
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Random Selections with no dulpicates
#1
Project Objective:
Make 5 random selections from a list of 25 items with no dultiples or numbers used more than one time.

Tried:
from random import sample
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
Chosennumber = sample(list, 5)
print("sampling with choices method ", Chosennumber)
Chosennumber = sample(list, 5)
print("sampling with choices method ", Chosennumber)
Chosennumber = sample(list, 5)
print("sampling with choices method ", Chosennumber)
Chosennumber = sample(list, 5)
print("sampling with choices method ", Chosennumber)
Chosennumber = sample(list, 5)
print("sampling with choices method ", Chosennumber)
and

 
import random
#sampling with replacement
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
#for list in list:
sampling = random.choices(list, k=5)
print("sampling with choices method ", sampling)
sampling = random.choices(list, k=5)
print("sampling with choices method ", sampling)
sampling = random.choices(list, k=5)
print("sampling with choices method ", sampling)
sampling = random.choices(list, k=5)
print("sampling with choices method ", sampling)
sampling = random.choices(list, k=5)
print("sampling with choices method ", sampling)
Reply
#2
Those methods don't alter the original list to take out the chosen from the next selection
You could use random.shuffle
Then slice 5 off the list, then repeat 4 more times on the remaining list.
Reply
#3
Never use list as name.

Developing Yoriz idea: one shuffle is as good as five shuffles. Therefore one can just slice shuffled list:

>>> from random import shuffle
>>> lst = list(range(1, 26))
>>> shuffle(lst)
>>> [lst[s:s+5] for s in range(0, len(lst), 5)]
[[13, 21, 8, 16, 14], [25, 4, 15, 19, 6], [20, 23, 11, 1, 9], [17, 10, 22, 5, 2], [7, 3, 12, 18, 24]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random nr. no repetition & printing multiple lines Joey 7 2,788 Feb-05-2020, 04:23 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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