Please code a random number generator in PYTHON which satisfies the following criteria: THE CODE SKELETON IS GIVEN BELOW
Posted: Fri May 20, 2022 12:05 pm
Please code a random number generator in PYTHON which satisfies
the following criteria:
THE CODE SKELETON IS GIVEN BELOW please use it:
from typing import Generator
def lcg(modulus: int, a: int, c: int, seed: int) ->
Generator[int, None, None]:
"""Linear congruential generator."""
while True:
seed = (a * seed + c) % modulus
yield seed
class RandomGen:
def __init__(self) -> None:
raise NotImplementedError()
def randint(self, k: int) -> int:
raise NotImplementedError()
if __name__ == "__main__":
Random_gen = lcg(pow(2,32), 134775813, 1, 0)
Random Number Generation For our game, we also need Random Number Generation. An easy way to implement this is with a Linear Congruential Generator. Some python code (copied from Wikipedia) is given below: from typing import Generator def lcg (modulus: int, a: int, c: int, seed: int) -> Generator(int, None, None]: "Linear congruential generator. while True: seed = (a * seed + c) modulus yield seed Random_gen = lcg (pow (2,32), 134775813, 1, 0) But for our game, we need to use the Random generator in a particular way. Your task is to implement a RandomGen class which implements two methods: init_(self, seed: int=0) -> None, and randint (self, k: int) -> int, which generates a random number from 1 to k inclusive with the following approach: First, generate 5 random numbers using the Icg method above, dropping the 16 least significant bits of each number. Generate a new number, which is 16 bits long and has a 1 in each bit if at least 3 of the 5 generated numbers have a 1 in this bit. Return this new number, modulo k, plus 1. This process is illustrated below, calling randint (74):
1: Five Random Numbers 1341408904 01001111111101000100011010001000 3916732889 11101001011101001001100111011001 Binary 4161854668 11111000000100001101110011001100 11272702 00000000101011000000000111111110 483725054 00011100110101010000111011111110 2: Remove 16 Least Significant Bits 0100111111110100 1110100101110100 1111100000010000 0000000010101100 0001110011010101 2322421233341401 <- # 1s in each column 3: Generate new number from # of 1s in each column 0100100011110100 = 18676 4: Modulok 18676 % 74 = 28+1=29
the following criteria:
THE CODE SKELETON IS GIVEN BELOW please use it:
from typing import Generator
def lcg(modulus: int, a: int, c: int, seed: int) ->
Generator[int, None, None]:
"""Linear congruential generator."""
while True:
seed = (a * seed + c) % modulus
yield seed
class RandomGen:
def __init__(self) -> None:
raise NotImplementedError()
def randint(self, k: int) -> int:
raise NotImplementedError()
if __name__ == "__main__":
Random_gen = lcg(pow(2,32), 134775813, 1, 0)
Random Number Generation For our game, we also need Random Number Generation. An easy way to implement this is with a Linear Congruential Generator. Some python code (copied from Wikipedia) is given below: from typing import Generator def lcg (modulus: int, a: int, c: int, seed: int) -> Generator(int, None, None]: "Linear congruential generator. while True: seed = (a * seed + c) modulus yield seed Random_gen = lcg (pow (2,32), 134775813, 1, 0) But for our game, we need to use the Random generator in a particular way. Your task is to implement a RandomGen class which implements two methods: init_(self, seed: int=0) -> None, and randint (self, k: int) -> int, which generates a random number from 1 to k inclusive with the following approach: First, generate 5 random numbers using the Icg method above, dropping the 16 least significant bits of each number. Generate a new number, which is 16 bits long and has a 1 in each bit if at least 3 of the 5 generated numbers have a 1 in this bit. Return this new number, modulo k, plus 1. This process is illustrated below, calling randint (74):
1: Five Random Numbers 1341408904 01001111111101000100011010001000 3916732889 11101001011101001001100111011001 Binary 4161854668 11111000000100001101110011001100 11272702 00000000101011000000000111111110 483725054 00011100110101010000111011111110 2: Remove 16 Least Significant Bits 0100111111110100 1110100101110100 1111100000010000 0000000010101100 0001110011010101 2322421233341401 <- # 1s in each column 3: Generate new number from # of 1s in each column 0100100011110100 = 18676 4: Modulok 18676 % 74 = 28+1=29