Question: Part Four - Implementing the Acorn Generator Create a class called Acorn. Instance Variables: one or more instance variables representing the state ( think about

Part Four - Implementing the Acorn Generator
Create a class called Acorn.
Instance Variables:
one or more instance variables representing the state (think about how many you need)
don't store extraneous values
Methods:
__init__(self, seed: list[int], M: int): constructor. Although
is often considered the seed and
are often considered the initial values, for programming ease, we'll lump them into a single list for the parameter (where the first element is
and so on). Ensure that seed is a list containing integers. If not, raise a TypeError. Verify that
; if not, raise a ValueError.
__iter__(self): method that returns the iterator
__next__(self): method that returns next numbers in sequence. This should be a list of integers including
(even though it will be the same). Raises StopIteration exception when seuqence is repeating.
get_state(self): function that returns the current state. The current state should be represented as a dictionary. This dictionary should have the following keys:
'vals' storing the list of integers representing the previous values
'M' storing the integer-valued modulus
Note: you don't need this dictionary to contain anything that would be beneficial for determining the period (if we reload the state, we'll assume it's starting from that point with no previous values)
set_state(self, state): function that takes in dictionary representing the state in the same format returned by getState and sets appropriate instance variables. As mentioned above, calling this should reset the random number generator as if it has not seen previous values.
A sample sequence with
, and a seed and values of [7,6,8,11,5]:
[7,6,8,11,5]->[7,(6+7)%23,(8+13)%23,(11+21)%23,(5+9)%23]=[7,13,21,9,14]
[7,13,21,9,14]->[7,(13+7)%23,(21+20)%23,(9+18)%23,(14+4)%23]=[7,20,18,4,18]
[7,20,18,4,18]->[7,(20+7)%23,(18+4)23,(4+22)%23,(18+3)%23]=[7,4,22,3,21]
[7,4,22,3,21]->[7,(4+7)%23,(22+11)%23,(3+10)%23,(21+13)%23]=[7,11,10,13,11]
[7,11,10,13,11]->[7,(11+7)%23,(10+18)%23,(13+5)%23,(11+18)%23]=[7,18,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!