From 48b8af975ac0d7d53594a4ffa2663e9d01ff5fd7 Mon Sep 17 00:00:00 2001 From: Mahendra Date: Wed, 21 Mar 2018 16:13:43 +0530 Subject: [PATCH] added random(sample, shuffle, randrange) methods in helper_random --- src/helper_random.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/helper_random.py b/src/helper_random.py index a0fb08f1..df2eec49 100644 --- a/src/helper_random.py +++ b/src/helper_random.py @@ -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)