queueing example: random number generator state should be part of DEVS state
This commit is contained in:
parent
4d550b3faa
commit
8f0ca935f8
1 changed files with 5 additions and 3 deletions
|
|
@ -4,13 +4,15 @@ import random
|
||||||
|
|
||||||
# Define the state of the generator as a structured object
|
# Define the state of the generator as a structured object
|
||||||
class GeneratorState:
|
class GeneratorState:
|
||||||
def __init__(self, gen_num):
|
def __init__(self, gen_num, seed=0):
|
||||||
# Current simulation time (statistics)
|
# Current simulation time (statistics)
|
||||||
self.current_time = 0.0
|
self.current_time = 0.0
|
||||||
# Remaining time until generation of new event
|
# Remaining time until generation of new event
|
||||||
self.remaining = 0.0
|
self.remaining = 0.0
|
||||||
# Counter on how many events to generate still
|
# Counter on how many events to generate still
|
||||||
self.to_generate = gen_num
|
self.to_generate = gen_num
|
||||||
|
# State of our random number generator
|
||||||
|
self.random = random.Random(seed)
|
||||||
|
|
||||||
class Generator(AtomicDEVS):
|
class Generator(AtomicDEVS):
|
||||||
def __init__(self, gen_param, size_param, gen_num):
|
def __init__(self, gen_param, size_param, gen_num):
|
||||||
|
|
@ -34,7 +36,7 @@ class Generator(AtomicDEVS):
|
||||||
self.state.remaining = float('inf')
|
self.state.remaining = float('inf')
|
||||||
else:
|
else:
|
||||||
# Still have to generate events, so sample for new duration
|
# Still have to generate events, so sample for new duration
|
||||||
self.state.remaining = random.expovariate(self.gen_param)
|
self.state.remaining = self.state.random.expovariate(self.gen_param)
|
||||||
return self.state
|
return self.state
|
||||||
|
|
||||||
def timeAdvance(self):
|
def timeAdvance(self):
|
||||||
|
|
@ -43,7 +45,7 @@ class Generator(AtomicDEVS):
|
||||||
|
|
||||||
def outputFnc(self):
|
def outputFnc(self):
|
||||||
# Determine size of the event to generate
|
# Determine size of the event to generate
|
||||||
size = max(1, int(random.gauss(self.size_param, 5)))
|
size = max(1, int(self.state.random.gauss(self.size_param, 5)))
|
||||||
# Calculate current time (note the addition!)
|
# Calculate current time (note the addition!)
|
||||||
creation = self.state.current_time + self.state.remaining
|
creation = self.state.current_time + self.state.remaining
|
||||||
# Output the new event on the output port
|
# Output the new event on the output port
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue