- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Random number generator help
Random number generator help
Random number generator help
Hi,
I need a random number generator in OM, I found one online, the code is:
Code:
function random "Pseudo random number generator"
input Integer seedIn[3] "Seed from last call";
output Real x "Random number between 0 and 1";
output Integer seedOut[3] "Modified seed for next call";
algorithm
seedOut[1] := rem((171*seedIn[1]), 30269);
seedOut[2] := rem((172*seedIn[2]), 30307);
seedOut[3] := rem((170*seedIn[3]), 30323);
// Zero is a poor seed, therefore substitute 1;
for i in 1:3 loop
if seedOut[i] == 0 then
seedOut[i] := 1;
end if;
end for;
x := rem((seedOut[1]/30269.0 + seedOut[2]/30307.0 + seedOut[3]/30323.0), 1.0);
end random;
Then when I try to call the random function by using another function:
Code:
function testRandom1
input Integer seed[3];
output Real x;
algorithm
for i in 1:10 loop
(x, seed) := random(seed);
Modelica.Utilities.Streams.print("x = " + String(x,significantDigits=16) + "\n");
end for;
end testRandom1;
When I call testRandom1 function with parameters, like this:
testRandom1({23,35,75})
There is no output to the screen. But if I remove that "Modelica.Utilities.Streams.print" statement, I get one return value, but there is no loop then.
Please help. Thanks.
Re: Random number generator help
Works for me.
Code:
$ cat a.mos ; omc a.mos
loadFile("a.mo");
loadModel(Modelica);
testRandom1({23,35,75});getErrorString();
/home/marsj/dev/trunk/build//bin/omc 1.6.0
true
true
x = 0.7490418042138446
x = 0.8643095911627987
x = 0.4815900479477273
x = 0.07248000599078153
x = 0.6682546425212224
x = 0.2268866442874202
x = 0.6882514204208467
x = 0.06556030683102243
x = 0.1382381189319646
x = 0.4670802318116736
0.467080231811674
""
How are you trying to use the function? And from what client? If you are using OMShell/OMEdit, do note that it will only display stdout from functions that are called during simulation.
Since you called the function using constant input, it would be evaluated before the simulation starts (because in Modelica a function may not have side-effects like printing to standard output ). You could try using a non-constant input or annotation(__OpenModelica_Impure = true) to defer evaluation of the function to runtime.
- sjoelund.se
- 1700 Posts
Re: Random number generator help
Start OMC in a terminal and let OMShell/OMEdit resume the session; then view the output in that terminal. OMEdit launches omc with Qt calls, which disable stdout.
- sjoelund.se
- 1700 Posts
Re: Random number generator help
Also, there is an undocumented vendor-specific annotation:
Code:
annotation(__OpenModelica_Impure = true)
that you can add to a function to force OMC to not consider a function call constant
- sjoelund.se
- 1700 Posts
Re: Random number generator help
Hi! I am working on developing a client-server model between OpenModelica (OM) and external hardware. For this, I have written a sample OM file, as given below:
Code:
model test
input Real x;
Real y;
equation
y = sqrt(x);
end test;
Now, I wish to modify the code such that x generates a random value at each instant. Accordingly, I will read those values from the client end and change the values of x that will ultimately affect the value of y. Could you please provide some insights on this?
Re: Random number generator help
SudhakarK wrote:
Now, I wish to modify the code such that x generates a random value at each instant. Accordingly, I will read those values from the client end and change the values of x that will ultimately affect the value of y. Could you please provide some insights on this?
That's not possible. Impure functions may only be called at event instants. So you need to use when sample(...) or something similar.
- sjoelund.se
- 1700 Posts
Re: Random number generator help
sjoelund.se wrote:
That's not possible. Impure functions may only be called at event instants. So you need to use when sample(...) or something similar.
Thanks for your response. Could you please elaborate on how to use when sample(...) for my task?
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Random number generator help