How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Python programming language

Table of Contents

Problem Statement

numbers as shown above. are as shown above

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Python programming language

This source code implements a custom random number generator based on the xorshift* algorithm. It defines aXorshift_star class that has some methods to generate random numbers.

Class Definition:

class Xorshift_star():
 ...

Constructor:

def __init__(self, seed=0):
  • The constructor initializes the state of the random number generator with the provided seed. If no seed is provided, it defaults to 0.

Seed Function:

def seed(self, num):
  • The seed function allows you to reset the state of the random number generator to the specified seed value.

Next Integer Function:

def next_int(self):
  • The next_int function generates a random integer between 0 and 2^32 (inclusive). It uses a bit-manipulation approach called Xorshift* to generate the random numbers.

Next Float Function:

def next_float(self):
  • The next_float function generates a random float between 0 and 1 (exclusive). It does this by dividing the result of next_int by 2^32.

Main Function:

if __name__ == '__main__':
 ...
  • This is the entry point of the program. It creates an instance of the random number generator, seeds it with a value, and generates and prints 5 random integers.

Histogram Generation:

hist = {i:0 for i in range(5)}
for i in range(100_000):
 hist[int(random_gen.next_float() * 5)] += 1
  • This code generates a histogram of the random floats generated by the random number generator. It creates a dictionary where the keys are integers from 0 to 4, representing the bins of the histogram. It then iterates 100,000 times, generating random floats and incrementing the count in the corresponding bin in the histogram.

Output:

The program prints the following output:

2076396755
1308877619
2475744978
1880944311
4128370748
{0: 19996, 1: 20120, 2: 19924, 3: 19969, 4: 19991}
  • The first 5 lines are the random integers generated and printed.

  • The last line shows the histogram of the random floats generated. The bins are evenly distributed, which indicates that the random number generator is producing numbers close to uniform distribution.

Source code in the python programming language

mask64 = (1 << 64) - 1
mask32 = (1 << 32) - 1
const = 0x2545F4914F6CDD1D



class Xorshift_star():
    
    def __init__(self, seed=0):
        self.state = seed & mask64

    def seed(self, num):
        self.state =  num & mask64
    
    def next_int(self):
        "return random int between 0 and 2**32"
        x = self.state
        x = (x ^ (x >> 12)) & mask64
        x = (x ^ (x << 25)) & mask64
        x = (x ^ (x >> 27)) & mask64
        self.state = x
        answer = (((x * const) & mask64) >> 32) & mask32 
        return answer
    
    def  next_float(self):
        "return random float between 0 and 1"
        return self.next_int() / (1 << 32)
    

if __name__ == '__main__':
    random_gen = Xorshift_star()
    random_gen.seed(1234567)
    for i in range(5):
        print(random_gen.next_int())
        
    random_gen.seed(987654321)
    hist = {i:0 for i in range(5)}
    for i in range(100_000):
        hist[int(random_gen.next_float() *5)] += 1
    print(hist)


  

You may also check:How to resolve the algorithm Filter step by step in the Raven programming language
You may also check:How to resolve the algorithm Farey sequence step by step in the Wren programming language
You may also check:How to resolve the algorithm Subtractive generator step by step in the C++ programming language
You may also check:How to resolve the algorithm Zebra puzzle step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Entropy/Narcissist step by step in the AutoHotkey programming language