openmodelicainterest  

Re: Random generator in Modelica

Mikael Hagernäs
Mon, 16 Mar 2009 04:40:35 -0700

Ok. Can you please explain to me how to use this random function. How do i
use this package

2009/3/15 Peter Fritzson <pe...@ida.liu.se>

>
> You can also write a random function
> in Modelica. See page 761 in my book.
>
> /Peter F
>
> Modelica.Math.Random
> This Random package placed as a preliminary subpackage of
> Math contains the following two random distribution functions
> random() for uniform distributions and normalvariate() for
> Normal distribution.
> Package header:
>
> package Random
> import Modelica.Math; // import might not be needed
> constant Real NV_MAGICCONST=4*exp(-0.5)/sqrt(2.0);
> type Seed = Real[3];
> Modelica.Math.Random.random
> Uniform distribution random function
> Distribution uniform between 0 and 1.
> function random "input random number generator with
> external storage of the seed"
> input Seed si "input random seed";
> output Real x "uniform random variate between 0 and 1";
> output Seed so "output random seed";
> algorithm
> so[1] := abs(rem((171 * si[1]),30269));
> so[2] := abs(rem((172 * si[2]),30307));
> so[3] := abs(rem((170 * si[3]),30323));
> // zero is a poor Seed, therfore substitute 1;
> if so[1] == 0 then
> so[1] := 1;
> end if;
> if so[2] == 0 then
> so[2] := 1;
> end if;
> if so[3] == 0 then
> so[3] := 1;
> end if;
> x := rem((so[1]/30269.0 +so[2]/30307.0 +
> so[3]/30323.0),1.0);
> end random;
>
> Modelica.Math.Random.normalvariate
> Normal distribution random function
> function normalvariate "normally distributed random
> variable"
> input Real mu "mean value";
> input Real sigma "standard deviation";
> input Seed si "input random seed";
> output Real x "gaussian random variate";
> output Seed so "output random seed";
> protected
> Seed s1, s2;
> Real z, zz, u1, u2;
> Boolean break=false;
> algorithm
> s1 := si;
> u2 := 1;
> while not break loop
> (u1,s2) := random(s1);
> (u2,s1) := random(s2);
> z := NV_MAGICCONST*(u1-0.5)/u2;
> zz := z*z/4.0;
> break := zz <= (- Math.log(u2));
> end while;
> x := mu + z*sigma;
> so := s1;
> end normalvariate;
>
>
>
> -----Original Message-----
> From: owner-openmodelicainter...@ida.liu.se
> [mailto:owner-openmodelicainter...@ida.liu.se] On Behalf Of Adrian Pop
> Sent: den 15 mars 2009 18:10
> To: openmodelicainterest@ida.liu.se
> Subject: Re: Random generator
>
>
> Hi Mikael,
>
> Basically you make an external
> function to return the random value.
> See an example how in OpenModelica1.4.5/testmodels/External*
>
> Also have a look at this thread (previously on OpenModelicaInterest):
>  http://thread.gmane.org/gmane.comp.misc.openmodelica/501/focus=507
>
> Cheers,
> Adrian Pop/
>
> Mikael Hagernäs wrote:
> > Hi!
> >
> > I need to create a white noise signal. Is there any function or
> > algorithm i can use to create random numbers?
>
>
>
>
>