Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

How to get the clock tick value

How to get the clock tick value

When generating a random number, I want to treat the clock value ( the current time) as the seed.
But I don't know how to get the clock value ( the current time) in OM.
Can someone help me?
Thank you very much.

Re: How to get the clock tick value

xiaolong,

Re: How to get the clock tick value

Hi

You find two examples in DrControl - Kalman filter how to model a random generator in Modelica

function Mathrandom
  input Real si1;  // {si1,si2,si3} and {so1,so2,so3} should be replaced by si and so with the
  input Real si2;  // type Seed once OMC can handle a function call with an array vector
  input Real si3;
  input Real tim;
  output Real x;
  output Real so1;
  output Real so2;
  output Real so3;
  Seed so;
  Seed si;
  algorithm
  si := {si1,si2,si3};
  so[1] := abs(rem((171*si[1]*exp(mod(tim-11,tim+13))),30269));   
  so[2] := abs(rem((172*si[2]*exp(mod(tim-5,tim+7))),30307));
  so[3] := abs(rem((170*si[3]*exp(mod(tim-23,tim+76))),30323));
  if so[1] < 1e-4 then
     so[1] := 1;
   end if;
  if so[2] < 1e-4 then
     so[2] := 1;
  end if;
  if so[3] < 1e-4 then
    so[3] := 1;
   end if;
  x := rem((so[1]/30269.0 +so[2]/30307.0 + so[3]/30323.0),1.0);
  so1 :=so[1];
  so2:= so[2];
  so3 := so[3];
  end Mathrandom;

-------
function Mathrandom "Pseudo random number generator"   
  input Integer si[3] "Seed from last call";   
  output Real x "Random number between 0 and 1";   
  output Integer so[3] "Modified seed for next call";
algorithm   
  so[1] := rem((171*si[1]), 30269);   
  so[2] := rem((172*si[2]), 30307);   
  so[3] := rem((170*si[3]), 30323);   // Zero is a poor seed, therefore substitute 1;   
  for i in 1:3 loop     
    if so[i] == 0 then       
      so[i] := 1;     
     end if;   
  end for;   
  x := rem((so[1]/30269.0 + so[2]/30307.0 + so[3]/30323.0), 1.0);
  annotation(__OpenModelica_Impure = true);
end Mathrandom;

regards
Mohsen

Re: How to get the clock tick value

Thank you for you answer !
It helps me a lot !
Thank you very much..

There are 0 guests and 0 other users also viewing this topic
You are here: