HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Pseudo Random Number Generator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomnumbergeneratorpseudo

Problem

I recently watched this video about the random number generation in Super Mario World. The technique that is used, as seen in the image below, multiplies one of the seeds by 5 and adds 1, the other seed is multiplied by 2, and then depending on whether the 4th and 7th bits are the same, 1 is added. So as stated in the video, the sequence of numbers will repeat after 27776 successive calls.

In my implementation of a pseudo random number generator, I have used 16 bit values for the two seeds to allow for a greater range of numbers, and my get_rand() function returns the two 16 bit strings joined together, resulting in a 32 bit number. This means that the sequence of numbers repeats after 526838144 successive calls, which is far greater than that achieved with the pseudo random number generator used in Super Mario World.

I have also created a rand_int() and a random() function that allow for better use of the numbers generated, these simply divide the number returned by 2 ** 32 over the difference between the integer range.

The PRNG Test.py is there only so that I can make sure that all the functions work as expected, and it seems to provide evenly split pseudo random numbers. So I am just after a review of the PRNG.py file, as it is that which I would like to optimize and improve.

PRNG.py

```
#Make sure Seeds.txt exists
try:
file = open('Seeds.txt')
except FileNotFoundError:
file = open('Seeds.txt', 'a+')
file.write('0\n0')

#Gets the values of the seeds from the file
values = file.readlines()
file.close()

S = int(values[0].rstrip('\n'))
T = int(values[1])

def seed(seed_value):
'''Resets the seed for the PRNG to make values predictable'''
global S, T

with open('Seeds.txt', 'w') as file:
file.write(str(seed) + '\n' + str(seed))
file.close()

S = seed
T = seed

def update_seeds(S, T):
'''Generates the next two seeds'''
S = 5 * S + 1

try: bit_11 = '{0:b}'.format(T)[-11]
except IndexError

Solution


  • The statement bit_11 = '{0:b}'.format(T)[-11] actually extracts bit 10, because the convention is to number bits from zero, while indexing from the end of string starts at [-1].



  • Performing bit manipulations in string format is clunky. Use bitwise operators instead. For example, to extract bit 10: T >> 10 & 1



  • Opening a file by a with statement ensures it will be closed. You can omit the close calls.



  • Strings have a zfill method that does the same as for i in range(16 - len(K)): K = '0' + K

Context

StackExchange Code Review Q#162542, answer score: 2

Revisions (0)

No revisions yet.