Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pick 2 random items from a Python set? [duplicate]

Tags:

python

random

People also ask

How do you pick a random item from a set in Python?

sample() can be used instead random. Both arguments and output for random. sample() are similar to random. chocies().

How do you select random names from a list without repetition in Python?

Using the choice() method in random module, the choice() method returns a single random item from a list, tuple, or string.

How do you do random sampling in Python?

sample() function. sample() is an inbuilt function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement.


Use the random module: http://docs.python.org/library/random.html

import random
random.sample(set([1, 2, 3, 4, 5, 6]), 2)

This samples the two values without replacement (so the two values are different).