2019-10-08 11:42:31 +02:00
|
|
|
"""Convenience functions for random operations. Not suitable for security / cryptography operations."""
|
2018-04-30 16:55:10 +02:00
|
|
|
|
2018-03-21 11:43:43 +01:00
|
|
|
import random
|
2021-05-16 21:05:52 +02:00
|
|
|
|
2020-01-24 15:03:13 +01:00
|
|
|
|
2018-03-21 13:49:08 +01:00
|
|
|
NoneType = type(None)
|
|
|
|
|
2017-07-05 08:52:16 +02:00
|
|
|
|
2019-08-01 13:37:26 +02:00
|
|
|
def seed():
|
|
|
|
"""Initialize random number generator"""
|
|
|
|
random.seed()
|
|
|
|
|
|
|
|
|
2018-03-21 11:43:43 +01:00
|
|
|
def randomshuffle(population):
|
|
|
|
"""Method randomShuffle.
|
|
|
|
|
|
|
|
shuffle the sequence x in place.
|
|
|
|
shuffles the elements in list in place,
|
|
|
|
so they are in a random order.
|
2018-03-21 14:56:27 +01:00
|
|
|
As Shuffle will alter data in-place,
|
|
|
|
so its input must be a mutable sequence.
|
|
|
|
In contrast, sample produces a new list
|
|
|
|
and its input can be much more varied
|
|
|
|
(tuple, string, xrange, bytearray, set, etc)
|
2018-03-21 11:43:43 +01:00
|
|
|
"""
|
2018-03-21 14:56:27 +01:00
|
|
|
random.shuffle(population)
|
2018-03-21 11:43:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
def randomsample(population, k):
|
|
|
|
"""Method randomSample.
|
|
|
|
|
|
|
|
return a k length list of unique elements
|
|
|
|
chosen from the population sequence.
|
|
|
|
Used for random sampling
|
2018-03-21 14:56:27 +01:00
|
|
|
without replacement, its called
|
|
|
|
partial shuffle.
|
2018-03-21 11:43:43 +01:00
|
|
|
"""
|
|
|
|
return random.sample(population, k)
|
|
|
|
|
|
|
|
|
2018-03-21 13:49:08 +01:00
|
|
|
def randomrandrange(x, y=None):
|
2018-03-21 11:43:43 +01:00
|
|
|
"""Method randomRandrange.
|
|
|
|
|
|
|
|
return a randomly selected element from
|
|
|
|
range(start, stop). This is equivalent to
|
|
|
|
choice(range(start, stop)),
|
|
|
|
but doesnt actually build a range object.
|
|
|
|
"""
|
2018-03-21 13:49:08 +01:00
|
|
|
if isinstance(y, NoneType):
|
2018-04-30 16:55:10 +02:00
|
|
|
return random.randrange(x) # nosec
|
2019-10-08 11:42:31 +02:00
|
|
|
return random.randrange(x, y) # nosec
|
2018-03-21 14:56:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
def randomchoice(population):
|
|
|
|
"""Method randomchoice.
|
|
|
|
|
|
|
|
Return a random element from the non-empty
|
|
|
|
sequence seq. If seq is empty, raises
|
|
|
|
IndexError.
|
|
|
|
"""
|
2018-04-30 16:55:10 +02:00
|
|
|
return random.choice(population) # nosec
|