How to use random.randint()
to select random names given in a list in python.
I want to print 5 names from that list. Actually i know how to use random.randint()
for numbers. but i don't know how to select random names from a given list.
we are not allowed to use random.choice
.
help me please
This will not guarantee no repeats, since random.choice is better for that.
If you want to get a random element from a list using
random.randint()
, below is an optionYes, for repeat sample from one population, @MaxLascombe's answer is OK. If you do not want tiles in samples, you should kick the chosen one out. Then use @MaxLascombe's answer on the rest of the list.
Are you allowed to use
random.sample
?Using solely
random.randint
would be more difficult, but if you must do so, I'd do something like this:But obviously this is with replacement. If you don't want replacement, you'd need some more complicated code. For example, a function:
So:
The simplest way to select a random item from the list is to use using
random.randint()
as below,Say for instance your list is:
and you want to print an item from the list randomly then,