Vensim-Python Random Number Generator

Use this forum to post Vensim related questions.
Post Reply
aliakhavan89
Senior Member
Posts: 198
Joined: Sun Oct 21, 2018 7:09 am
Vensim version: DSS

Vensim-Python Random Number Generator

Post by aliakhavan89 »

Is it possible to reproduce Vensim random number generation process in Python?

If I have only the following variable in a model (attached):

Code: Select all

RANDOM NORMAL( -1e+07 , 1e+07 , 0 , 0.5 , 41 )
And I use the following code:

Code: Select all

import numpy as np

np.random.seed(41)

output = []

for i in range(100):
    test = np.random.normal(0, 0.5)
    truncated_test = np.clip(test, -1e+7, 1e+7)
    output.append(truncated_test)

I don't get the same results:
Screenshot 2024-12-23 at 10.50.54 AM.png
Screenshot 2024-12-23 at 10.50.54 AM.png (1.13 MiB) Viewed 5376 times

As far as I know, both Vensim and Python use Mersenne Twister algorithm for random number generation. Could Vensim lower precision cause any differences?
Attachments
test.mdl
(1.11 KiB) Downloaded 210 times
Administrator
Super Administrator
Posts: 4834
Joined: Wed Mar 05, 2003 3:10 am

Re: Vensim-Python Random Number Generator

Post by Administrator »

aliakhavan89 wrote: Mon Dec 23, 2024 4:06 pmIs it possible to reproduce Vensim random number generation process in Python?
Probably not.
aliakhavan89 wrote: Mon Dec 23, 2024 4:06 pmI don't get the same results:
This is not surprising.
aliakhavan89 wrote: Mon Dec 23, 2024 4:06 pmAs far as I know, both Vensim and Python use Mersenne Twister algorithm for random number generation. Could Vensim lower precision cause any differences?
Lower precision? Vensim works with double precision numbers, I suspect Python does as well.

You would need to set the starting seed in Python to the same one that is in Vensim. And even then, if numpy does not use the exact same code we do, it's unlikely you'll get the same results.

Vensim uses the SIMD implementation of Mersenne Twister,
https://www.vensim.com/documentation/le ... _simd.html
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
tomfid
Administrator
Posts: 3993
Joined: Wed May 24, 2006 4:54 am

Re: Vensim-Python Random Number Generator

Post by tomfid »

Even if the code were identical, it might be hard to get them to match - they'd have to be using the same Mersenne prime internally, and the number of draws has to be identical (Vensim makes an extra draw during model initialization). Also, Vensim adds an arbitrary integer offset to the seed.

A more sensible test would be to draw a million numbers from each and use the Kolmogorov-Smirnov test (or similar) to verify that they have the expected distribution. We did this with the NIST test suite.
aliakhavan89
Senior Member
Posts: 198
Joined: Sun Oct 21, 2018 7:09 am
Vensim version: DSS

Re: Vensim-Python Random Number Generator

Post by aliakhavan89 »

These are great details. Using the KS metrics makes sense. Thank you!
Post Reply