10 examples of 'random.choice python' in Python

Every line of 'random.choice python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
273def choice(self, seq):
274 """Choose a random element from a non-empty sequence."""
275 return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

84def choice(*args, **kwargs):
85 return _default.choice(*args, **kwargs)
92def randSelect(self, seq):
93 """
94 Randomly select an element from a sequence. If the argument
95 'seq' is a dict, a randomly selected key will be returned.
96
97 :param seq: sequence to randomly select from
98 :type seq: list
99 :returns: element from seq if seq is a list, a key if seq
100 is a dict, or None if seq is empty
101 """
102 if isinstance(seq, dict):
103 selection = RandomGen.randGen.choice(list(seq.keys()))
104
105 elif seq:
106 selection = RandomGen.randGen.choice(seq)
107 else:
108 selection = None
109
110 return selection
67def weighted_choice(choices):
68 total = sum(w for c, w in choices)
69 r = random.uniform(0, total)
70 upto = 0
71 for c, w in choices:
72 if upto + w >= r:
73 return c
74 upto += w
75 assert False, "Shouldn't get here"
48def random_choice(image_size):
49 height, width = image_size
50 crop_height, crop_width = 320, 320
51 x = random.randint(0, max(0, width - crop_width))
52 y = random.randint(0, max(0, height - crop_height))
53 return x, y
33def gen_random():
34 return random.random()
14def random(size):
15 return rand(*size)
87def random_choice(start, end , _multiply ):
88 start = start * _multiply
89 end = end * _multiply
90 num = random.randrange(start,end + 1 ,1)
91 #print ("il num e'",num/_multiply)
92 return float( num / _multiply)
165def weighted_choice(items):
166 '''
167 Like random.choice but the items have weights to decide which is
168 more likely to be choosen.
169
170 Format: List of tuples. Tuple should have item first, then its weight i.e. [(item, weight), (item, weight)]
171 '''
172 total = sum([weight for item, weight in items])
173 threshold = random.uniform(0, total)
174
175 for item, weight in items:
176 # Lower the threshold each time through the loop
177 threshold -= weight
178
179 # If threshold met then item picked
180 if threshold <= 0:
181 return item
182 assert False, "Unreachable code point"
45def take_random(seq):
46 elem = random.choice(seq)
47 seq.remove(elem)
48 return elem

Related snippets