added random(sample, shuffle, randrange) methods in helper_random

This commit is contained in:
Mahendra 2018-03-21 16:13:43 +05:30
parent ef1cd5a9bd
commit 48b8af975a
No known key found for this signature in database
GPG Key ID: A672D8FAAEE398B3
1 changed files with 33 additions and 1 deletions

View File

@ -1,9 +1,41 @@
import os
import random
from pyelliptic.openssl import OpenSSL
def randomBytes(n):
"""Method randomBytes."""
try:
return os.urandom(n)
except NotImplementedError:
return OpenSSL.rand(n)
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)