Created
March 1, 2014 19:38
-
-
Save MrDHat/9295894 to your computer and use it in GitHub Desktop.
Random Number Generator in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# imports for abstract classes | |
from abc import ABCMeta, abstractmethod | |
import time | |
''' ============================================================================================== | |
Seed Classes | |
===============================================================================================''' | |
class Seed(object): | |
"""Abstract class for seeds""" | |
__metaclass__ = ABCMeta | |
@abstractmethod | |
# Function that generates seed | |
def generate_seed(self): | |
pass | |
class TimeSeed(Seed): | |
""" Generates seed from current time """ | |
def generate_seed(self): | |
return time.time() | |
''' ============================================================================================== | |
End Seed Classes | |
===============================================================================================''' | |
''' ================================================================================================== | |
Randomizer Classes | |
===================================================================================================''' | |
class Randomizer(object): | |
"""Abstract class to generate random numbers""" | |
__metaclass__ = ABCMeta | |
@abstractmethod | |
# Function that generates random numbers | |
# 'Decorate' seed onto the random number generator | |
def random(self, start, end, seed): | |
pass | |
class XORShiftRandomizer(Randomizer): | |
"""XOR Shift Randomizer""" | |
def random(self, start, end, seed): | |
random_number = seed.generate_seed() | |
# Get number after decimal point of seed because these are the numbers that actually vary | |
random_number = random_number % 1 | |
random_number = str(random_number) | |
# Check if the number is decimal first | |
if random_number.find('.') != -1: | |
random_number = random_number.split('.')[1] | |
# Do not split if no decimal point and just take the integer as it is | |
random_number = int(random_number) | |
random_number ^= (random_number << 21); | |
random_number ^= (random_number >> 35); | |
random_number ^= (random_number << 4); | |
# Convert the generated number to lie between start and end | |
random_number = random_number % end | |
if random_number < start: | |
random_number = random_number + start | |
return random_number | |
''' ================================================================================================== | |
End Randomizer Classes | |
===================================================================================================''' | |
if __name__ == "__main__": | |
number_of_numbers = int(raw_input()) | |
number_of_digits = int(raw_input()) | |
xor_random = XORShiftRandomizer() | |
if number_of_numbers == 0: | |
exit(0) | |
# Calculate start and end of the range from number of digits | |
# start and end are starting as string because 0/9 will be appended to them and start and end will be generated. They will later be converted to integer | |
start = "1" | |
end = "9" | |
if number_of_digits == 0: | |
exit(0) | |
elif number_of_digits == 1: | |
start = 0 | |
else: | |
while number_of_digits > 1: | |
start = start + "0" | |
end = end + "9" | |
number_of_digits = number_of_digits - 1 | |
start = int(start) | |
end = int(end) | |
while number_of_numbers > 0: | |
print xor_random.random(start, end, TimeSeed()) | |
number_of_numbers = number_of_numbers - 1 |
Hey,
What are the lines 64, 65 and 66 for?
lines 64, 65 and 66 are the lines that actually perform the randomisation operation. They are actually the core of randomisation function.
I've used XORShift randomisation technique which performs an "OR" operation between the number with its 'shifts'. The numbers 21, 35 and 4 are known to provide best possible results.
Note: This is not cryptographically secure but generates a true random number which can be used for generating random ID's etc.
Just curious, what do you think about this article touting further speed improvements: http://xorshift.di.unimi.it/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
$ python random.py
Input:
1st line: No of random numbers required
2nd line: No of digits of random number required
For example:
Output: