question

xuan-123 avatar image
0 Likes"
xuan-123 asked Jason Lightfoot commented

How to randomly pick a random seed to perform a flexsim simulation?

I use python to open flexsim and simulate it once. I will open multiple flexsims, and each flexsim will only reset run once. But because the random seeds are fixed at the first one, my multiple flexsim simulation results are the same.


I hope that when the model is opened, a random seed is randomly selected to execute the simulation of the model, instead of only running the random seed of the first simulation every time it is opened.


How can I write flexscript to achieve this?

My current idea is to use On Model Open Trigger to execute a randomly generated random seed environment, but I don't know how to write the program.


I have referred to this article.

https://answers.flexsim.com/questions/92158/how-to-set-seed-of-random-functions.html


Thank you for your reply!


FlexSim 23.0.10
flexsim scriptrandom streamrandom seed
· 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.

Jason Lightfoot avatar image Jason Lightfoot ♦ commented ·

Hi @xuan-123, was Kavika F's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes 0 ·

1 Answer

·
Kavika F avatar image
0 Likes"
Kavika F answered Kavika F commented

If you go into the Statistics Settings, you can uncheck "Repeat Random Streams".

1702317379188.png

Then, when you reset the model, the random stream seed will change. Random events will be different each time you reset and run the model.


1702317379188.png (6.7 KiB)
· 10
5 |100000

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

xuan-123 avatar image xuan-123 commented ·

I tried this and it doesn't work

This method is effective for multiple resets and runs within a model.

But my problem is that I open a new flexsim window every time and only execute it once.

1702318596349.png

0 Likes 0 ·
1702318596349.png (1.3 MiB)
Jason Lightfoot avatar image Jason Lightfoot ♦ xuan-123 commented ·

It would be better that you set the replication number by which all seeds will be set. This way you can repeat your trials by just choosing the replication number in the experimenter gui and setting parameters to match.

Set this node value before a reset:

MODEL:/Tools/randomrepeatrep


0 Likes 0 ·
Kavika F avatar image Kavika F ♦ xuan-123 commented ·

@Xuan_123, you can try receiving a random number from python using User Commands (reference this post on setting up a Python User Command). You can then setup an "OnModelOpen" trigger to run get 2 random numbers from python. If your command was called "MyRandomNumber()", your trigger code may look like this:

int stream = MyRandomNumber();
int seed = MyRandomNumber();
randinit(stream, seed);

With your python code looking like this:

import random

def random_number():
    return random.randint(0, 2000)

random_number.py

random_example.fsm

0 Likes 0 ·
xuan-123 avatar image xuan-123 Kavika F ♦ commented ·

I previously tried to write On Model Open like this to test the model, but the entry parcel for the first simulation under different windows was still 3.

I think this is the same approach as you suggested. I don’t know if I misunderstood.

1702321904126.png

0 Likes 0 ·
1702321904126.png (15.2 KiB)
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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