2017-07-05 08:52:16 +02:00
|
|
|
import os
|
2018-03-21 11:43:43 +01:00
|
|
|
import random
|
2017-07-05 08:52:16 +02:00
|
|
|
from pyelliptic.openssl import OpenSSL
|
|
|
|
|
|
|
|
def randomBytes(n):
|
2018-03-21 11:43:43 +01:00
|
|
|
"""Method randomBytes."""
|
2017-07-05 08:52:16 +02:00
|
|
|
try:
|
|
|
|
return os.urandom(n)
|
|
|
|
except NotImplementedError:
|
|
|
|
return OpenSSL.rand(n)
|
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.
|
|
|
|
"""
|
|
|
|
return random.shuffle(population)
|
|
|
|
|
|
|
|
|
|
|
|
def randomsample(population, k):
|
|
|
|
"""Method randomSample.
|
|
|
|
|
|
|
|
return a k length list of unique elements
|
|
|
|
chosen from the population sequence.
|
|
|
|
Used for random sampling
|
|
|
|
without replacement
|
|
|
|
"""
|
|
|
|
return random.sample(population, k)
|
|
|
|
|
|
|
|
|
|
|
|
def randomrandrange(x, y):
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
return random.randrange(x, y)
|