1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import random as random_module
def several_random(value, arg=1):
"Returns one or more random item(s) from the list"
try:
arg = int(arg)
except ValueError:
return value
if arg == 1:
return random_module.choice(value)
elif len(value) > arg: # Only pick if we are asked for fewer items than we are given
return random_module.sample(value, arg)
else: # Number requested is equal to or greater than the number we have, return them all in random order
new_list = list(value)
random_module.shuffle(new_list)
return new_list
|
More like this
- SelectRelatedManager by realmac 4 months, 3 weeks ago
- Pagination/Filtering Alphabetically by zain 2 years, 11 months ago
- Split a string to a list and add to select options by xuqingkuang 2 years, 9 months ago
- Real shuffle function by marinho 1 year, 8 months ago
- Use dateutil's relativedelta to sort an arbitrary list of Django objects by date by cmcavoy 4 years, 11 months ago
Comments