question

Jouky D avatar image
0 Likes"
Jouky D asked Jeanette F commented

How to set seed of random functions?

Hello everyone,

I have 4 probability distributions in the model. I have seen some models that put getstream(activity), I do not know how it works. I only want to set a seed to all random numbers to replicate the experiment whenever I want (in R works as: set.seed(1234) and I can change the 1234 seed to 15172 if I want)).

I have read about a function that is randinit(0,375) but I do not know how to execute my model with this stream; if I execute the process flow model after executing it and unchecking the option Repeat Random Streams (I want to repeat the seed, but I want to ensure that is the seed I have put what I am replicating).

How can I set a seed and change it whatever I want?

Thank you for your time.

FlexSim 20.0.2
processflowflexsim 20.0.2random streamrepeat random streamssetseed
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Phil BoBo avatar image
1 Like"
Phil BoBo answered Phil BoBo commented

The raninit() command is the correct way to set the seed value of a random number stream.

Using the Toolbox window, add Modeling Logic > Model Trigger > On Reset to your model.

In the OnModelReset trigger, execute the randinit() command to reset the seed value for whatever streams you want to set. For example:

randinit(8, 1234);

Then, when you sample a distribution from that stream, it will be sampled using the specified seed. For example, if you then subsequently call the following code, it will return 0.1160 because you initialized stream 8 to seed 1234:

return uniform(0, 1, 8);

Subsequent distribution calls using stream 8 will continue to sample from that stream that had been initialized with seed 1234.

Attached is a simple example using Process Flow. randinit_example.fsm

If you reset the model and then press Step twice, the token will be created at time 4.53. If you then comment out the randinit() call in OnModelReset, apply it, reset the model, and then press Step twice, the token will be created at a different time based on the default seed value of stream 8. If you then uncomment the randinit() call, apply, reset the model, and then press Step twice again, you'll see that the first event is again at 4.53 because that's what the distribution returns when sampling from the stream that was initialized to seed 1234.

The getstream() command returns a stream number that is unique for the object that you passed to it.

When running a simulation, it is a good practice to use different random number streams for different objects and activities. This is discussed at length in various Discrete Event Simulation textbooks. The getstream() command is an easy way to accomplish that result. FlexSim will initialize those streams automatically (if Repeat Random Streams is checked).

If you want direct control over the streams and seeds you use, then you should pass the particular stream and seed values you want to the various distribution functions in your model, as shown in the example above that uses stream 8 initialized with seed 1234.

If you run the above example, you will get the exact same results every time, including when using the experimenter. To get different results in the experimenter, you'll need to initialize your streams in the Start of Replication trigger to different seed values depending on the scenario and replication number.

If you build your entire model this way with a different stream number for each activity and object, calling randinit() in On Model Reset and Start of Replication to initialize each of those random number streams to unique seed values for each scenario/replication combination, then you will have replicated what FlexSim does automatically for you when simply using the Repeat Random Number Streams checkbox and the getstream() command.


1602776180052.png (149.4 KiB)
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jouky D avatar image Jouky D commented ·

Thank you for you answer! It was very useful! But now when I simulate with the Experimenter, all the replications have the same seed. How can I set a different seed (but controlled) for each replication? For example:

Replication 1: seed=1234

Replication 2: seed=1234+1=1235

Replication 3: seed=1236

etc.
I want to do the experimentations using controlled but different seeds.

Thank you!

0 Likes 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Jouky D commented ·

As I stated in the original answer: "To get different results in the experimenter, you'll need to initialize your streams in the Start of Replication trigger to different seed values depending on the scenario and replication number. "

1 Like 1 ·
1603116814662.png (72.3 KiB)
nmercogl avatar image
0 Likes"
nmercogl answered Jeanette F commented

Hello everyone, I found this exchange of questions and answers very helpful. I was wondering if it would be possible to set a seed controlled by the modeler, as illustrated by you, but not as a sequential integer (like 1234, 1235, 1236, ...). I would like to have full control over the seed I input for each replication of a scenario.

For example:

Replication1 ---> randinit(8, 100);

Replication1 ---> randinit(8, 1054);

Replication1 ---> randinit(8, 23);

...

Thank you!

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jeanette F avatar image Jeanette F ♦♦ commented ·
Please follow our best practices and start a new post.
0 Likes 0 ·