/******************************************************************************* Copyright by Telesoft Europe AB 1990, 1991. Copyright by Telelogic Malmoe AB 1991, 1992, 1993, 1994. Copyright by Telelogic AB 1994 - 2001. This Program is owned by Telelogic and is protected by national copyright laws and international copyright treaties. Telelogic grants you the right to use this Program on one computer or in one local computer network at any one time. Under this License you may only modify the source code for the purpose of adapting it to your environment. You must reproduce and include any copyright and trademark notices on all copies of the source code. You may not use, copy, merge, modify or transfer the Program except as provided in this License. Telelogic does not warrant that the Program will meet your requirements or that the operation of the Program will be uninterrupted and error free. You are solely responsible that the selection of the Program and the modification of the source code will achieve your intended results and that the results are actually obtained. *******************************************************************************/ /* # clearcase random.pr@@/main/5 : 2001/07/31 jk */ NEWTYPE RandomControl /*#NAME 'RandomControl'*/ OPERATORS Random /*#NAME 'Random'*/ : RandomControl -> Real; Random /*#NAME 'RandomT'*/ : RandomControl -> Duration; Random /*#NAME 'RandomT'*/ : RandomControl -> Time; Erlang /*#NAME 'Erlang'*/ : Real, Integer, RandomControl -> Real; Erlang /*#NAME 'ErlangRT'*/ : Real, Integer, RandomControl -> Duration; Erlang /*#NAME 'ErlangRT'*/ : Real, Integer, RandomControl -> Time; Erlang /*#NAME 'ErlangTR'*/ : Duration, Integer, RandomControl -> Real; Erlang /*#NAME 'ErlangTT'*/ : Duration, Integer, RandomControl -> Duration; Erlang /*#NAME 'ErlangTT'*/ : Duration, Integer, RandomControl -> Time; Erlang /*#NAME 'ErlangTR'*/ : Time, Integer, RandomControl -> Real; Erlang /*#NAME 'ErlangTT'*/ : Time, Integer, RandomControl -> Duration; Erlang /*#NAME 'ErlangTT'*/ : Time, Integer, RandomControl -> Time; HyperExp2 /*#NAME 'HyperExp2' */ : Real, Real, Real, RandomControl -> Real; HyperExp2 /*#NAME 'HyperExp2RT' */ : Real, Real, Real, RandomControl -> Duration; HyperExp2 /*#NAME 'HyperExp2RT' */ : Real, Real, Real, RandomControl -> Time; HyperExp2 /*#NAME 'HyperExp2TR' */ : Duration, Duration, Real, RandomControl -> Real; HyperExp2 /*#NAME 'HyperExp2TT' */ : Duration, Duration, Real, RandomControl -> Duration; HyperExp2 /*#NAME 'HyperExp2TT' */ : Duration, Duration, Real, RandomControl -> Time; HyperExp2 /*#NAME 'HyperExp2TR' */ : Time, Time, Real, RandomControl -> Real; HyperExp2 /*#NAME 'HyperExp2TT' */ : Time, Time, Real, RandomControl -> Duration; HyperExp2 /*#NAME 'HyperExp2TT' */ : Time, Time, Real, RandomControl -> Time; NegExp /*#NAME 'NegExp' */ : Real, RandomControl -> Real; NegExp /*#NAME 'NegExpRT' */ : Real, RandomControl -> Duration; NegExp /*#NAME 'NegExpRT' */ : Real, RandomControl -> Time; NegExp /*#NAME 'NegExpTR' */ : Duration, RandomControl -> Real; NegExp /*#NAME 'NegExpTT' */ : Duration, RandomControl -> Duration; NegExp /*#NAME 'NegExpTT' */ : Duration, RandomControl -> Time; NegExp /*#NAME 'NegExpTR' */ : Time, RandomControl -> Real; NegExp /*#NAME 'NegExpTT' */ : Time, RandomControl -> Duration; NegExp /*#NAME 'NegExpTT' */ : Time, RandomControl -> Time; Uniform /*#NAME 'Uniform' */ : Real, Real, RandomControl -> Real; Uniform /*#NAME 'UniformRT' */ : Real, Real, RandomControl -> Duration; Uniform /*#NAME 'UniformRT' */ : Real, Real, RandomControl -> Time; Uniform /*#NAME 'UniformTR' */ : Duration, Duration, RandomControl -> Real; Uniform /*#NAME 'UniformTT' */ : Duration, Duration, RandomControl -> Duration; Uniform /*#NAME 'UniformTT' */ : Duration, Duration, RandomControl -> Time; Uniform /*#NAME 'UniformTR' */ : Time, Time, RandomControl -> Real; Uniform /*#NAME 'UniformTT' */ : Time, Time, RandomControl -> Duration; Uniform /*#NAME 'UniformTT' */ : Time, Time, RandomControl -> Time; Draw /*#NAME 'Draw' */ : Real, RandomControl -> Boolean; Geometric /*#NAME 'Geometric' */ : Real, RandomControl -> Integer; Geometric /*#NAME 'GeometricRT' */ : Real, RandomControl -> Duration; Geometric /*#NAME 'GeometricRT' */ : Real, RandomControl -> Time; Geometric /*#NAME 'GeometricTI' */ : Duration, RandomControl -> Integer; Geometric /*#NAME 'GeometricTT' */ : Duration, RandomControl -> Duration; Geometric /*#NAME 'GeometricTT' */ : Duration, RandomControl -> Time; Geometric /*#NAME 'GeometricTI' */ : Time, RandomControl -> Integer; Geometric /*#NAME 'GeometricTT' */ : Time, RandomControl -> Duration; Geometric /*#NAME 'GeometricTT' */ : Time, RandomControl -> Time; Poisson /*#NAME 'Poisson' */ : Real, RandomControl -> Integer; Poisson /*#NAME 'PoissonRT' */ : Real, RandomControl -> Duration; Poisson /*#NAME 'PoissonRT' */ : Real, RandomControl -> Time; Poisson /*#NAME 'PoissonTI' */ : Duration, RandomControl -> Integer; Poisson /*#NAME 'PoissonTT' */ : Duration, RandomControl -> Duration; Poisson /*#NAME 'PoissonTT' */ : Duration, RandomControl -> Time; Poisson /*#NAME 'PoissonTI' */ : Time, RandomControl -> Integer; Poisson /*#NAME 'PoissonTT' */ : Time, RandomControl -> Duration; Poisson /*#NAME 'PoissonTT' */ : Time, RandomControl -> Time; RandInt /*#NAME 'RandInt' */ : Integer, Integer, RandomControl -> Integer; RandInt /*#NAME 'RandIntT' */ : Integer, Integer, RandomControl -> Duration; RandInt /*#NAME 'RandIntT' */ : Integer, Integer, RandomControl -> Time; DefineSeed /*#NAME 'DefineSeed'*/ : Integer -> RandomControl; Seed /*#NAME 'Seed'*/ : RandomControl -> Integer; /*#ADT(TA(S)E(S)W(H)R(H)H) #TYPE typedef unsigned long * RandomControl; #define xRandomTraceLevel xtNine #HEADING #ifdef __cplusplus #include #endif extern double log XPP(( double )); extern double exp XPP(( double )); extern SDL_Real Random XPP((RandomControl Seq)); #define RandomT(P1) RtoT(Random(P1)) extern SDL_Real Erlang XPP((SDL_Real Mean, SDL_Integer r, RandomControl Seq)); #define ErlangRT(P1, P2, P3) RtoT(Erlang(P1, P2, P3)) #define ErlangTR(P1, P2, P3) Erlang(TtoR(P1), P2, P3) #define ErlangTT(P1, P2, P3) RtoT(Erlang(TtoR(P1), P2, P3)) extern SDL_Real HyperExp2 XPP((SDL_Real Mean1, SDL_Real Mean2, SDL_Real Alpha, RandomControl Seq)); #define HyperExp2RT(P1, P2, P3, P4) RtoT(HyperExp2(P1, P2, P3, P4)) #define HyperExp2TR(P1, P2, P3, P4) HyperExp2(TtoR(P1), TtoR(P2), P3, P4) #define HyperExp2TT(P1, P2, P3, P4) RtoT(HyperExp2(TtoR(P1), TtoR(P2), P3, P4)) extern SDL_Real NegExp XPP((SDL_Real Mean, RandomControl Seq)); #define NegExpRT(P1, P2) RtoT(NegExp(P1, P2)) #define NegExpTR(P1, P2) NegExp(TtoR(P1), P2) #define NegExpTT(P1, P2) RtoT(NegExp(TtoR(P1), P2)) extern SDL_Real Uniform XPP((SDL_Real Low, SDL_Real High, RandomControl Seq)); #define UniformRT(P1, P2, P3) RtoT(Uniform(P1, P2, P3)) #define UniformTR(P1, P2, P3) Uniform(TtoR(P1), TtoR(P2), P3) #define UniformTT(P1, P2, P3) RtoT(Uniform(TtoR(P1), TtoR(P2), P3)) extern SDL_Real Draw XPP((SDL_Real Alpha, RandomControl Seq)); extern SDL_Integer Geometric XPP((SDL_Real p, RandomControl Seq)); #define GeometricRT(P1, P2) ItoT(Geometric(P1, P2)) #define GeometricTI(P1, P2) Geometric(TtoR(P1), P2) #define GeometricTT(P1, P2) ItoT(Geometric(TtoR(P1), P2)) extern SDL_Integer Poisson XPP((SDL_Real m, RandomControl Seq)); #define PoissonRT(P1, P2) ItoT(Poisson(P1, P2)) #define PoissonTI(P1, P2) Poisson(TtoR(P1), P2) #define PoissonTT(P1, P2) ItoT(Poisson(TtoR(P1), P2)) extern SDL_Integer RandInt XPP((SDL_Integer Low, SDL_Integer High, RandomControl Seq)); #define RandIntT(P1, P2, P3) ItoT(RandInt(P1, P2, P3)) extern RandomControl DefineSeed XPP((SDL_Integer Seed)); extern SDL_Integer Seed XPP((RandomControl Seq)); extern SDL_Real TtoR XPP((SDL_Time D)); extern SDL_Time RtoT XPP((SDL_Real R)); extern SDL_Time ItoT XPP((SDL_Integer I)); extern char * yWri_RandomControl XPP((void * Value)); extern int yRead_RandomControl XPP((void * Result)); #BODY SDL_Real TtoR( SDL_Time D ) { return (SDL_Real)D.s + (SDL_Real)D.ns*1E-9; } SDL_Time RtoT( SDL_Real R ) { SDL_Time D; D.s = (xint32)R; D.ns = (xint32)((R - (SDL_Real)D.s)*1E9); return D; } SDL_Time ItoT( SDL_Integer I ) { SDL_Time D; D.s = I; D.ns = (xint32)0; return D; } SDL_Real Random( RandomControl Seq ) { #ifdef XTRACE if ( xShouldBeTraced(xRandomTraceLevel, (xPrsNode)0) ) xPrintString(" Random\n"); #endif *Seq = *Seq + (*Seq<<1) + (*Seq<<16); return (SDL_Real)((*Seq)/4294967296.0); } SDL_Real Erlang( SDL_Real Mean, SDL_Integer r, RandomControl Seq ) { SDL_Real Prod; int I; #ifdef XTRACE if ( xShouldBeTraced(xRandomTraceLevel, (xPrsNode)0) ) xPrintString(" Erlang\n"); #endif #ifdef XOPERRORF if ( (Mean <= 0.0) || (r < 1) ) { xSDLOpError("Erlang in sort RandomControl", "Illegal parameter"); return 0.0; } #endif Prod = Random(Seq); for (I=2; I<=r; I++) Prod = Prod*Random(Seq); return (-log(Prod)*Mean / r); } SDL_Real HyperExp2 ( SDL_Real Mean1, SDL_Real Mean2, SDL_Real Alpha, RandomControl Seq ) { SDL_Real NegExp1; #ifdef XTRACE if ( xShouldBeTraced(xRandomTraceLevel, (xPrsNode)0) ) xPrintString(" HyperExp2\n"); #endif #ifdef XOPERRORF if ( (Mean1<=0.0) || (Mean2<=0.0) || (Alpha<0.0) || (Alpha>1.0) ) { xSDLOpError("HyperExp2 in sort RandomControl", "Illegal parameter"); return 0.0; } #endif NegExp1 = -log(Random(Seq)); if ( Alpha > Random(Seq) ) return NegExp1*Mean1; return NegExp1*Mean2; } SDL_Real NegExp( SDL_Real Mean, RandomControl Seq ) { #ifdef XTRACE if ( xShouldBeTraced(xRandomTraceLevel, (xPrsNode)0) ) xPrintString(" NegExp\n"); #endif #ifdef XOPERRORF if ( Mean <= 0.0 ) { xSDLOpError("NegExp in sort RandomControl", "Illegal parameter"); return 0.0; } #endif return -log(Random(Seq)) * Mean; } SDL_Real Uniform( SDL_Real Low, SDL_Real High, RandomControl Seq ) { #ifdef XTRACE if ( xShouldBeTraced(xRandomTraceLevel, (xPrsNode)0) ) xPrintString(" Uniform\n"); #endif #ifdef XOPERRORF if ( Low > High ) { xSDLOpError("Uniform in sort RandomControl", "Illegal parameter"); return 0.0; } #endif return Low + (High-Low)*Random(Seq); } SDL_Real Draw( SDL_Real Alpha, RandomControl Seq ) { #ifdef XTRACE if ( xShouldBeTraced(xRandomTraceLevel, (xPrsNode)0) ) xPrintString(" Draw\n"); #endif #ifdef XOPERRORF if ( (Alpha < 0.0) || (Alpha > 1.0) ) { xSDLOpError("Draw in sort RandomControl", "Illegal parameter"); return SDL_False; } #endif return Alpha > Random(Seq); } SDL_Integer Geometric( SDL_Real p, RandomControl Seq ) { #ifdef XTRACE if ( xShouldBeTraced(xRandomTraceLevel, (xPrsNode)0) ) xPrintString(" Geometric\n"); #endif #ifdef XOPERRORF if ( (p < 0.0) || (p >= 1.0) ) { xSDLOpError("Geometric in sort RandomControl", "Illegal parameter"); return 0; } #endif if ( p == 0.0 ) { (void)Random(Seq); return 0; } return (SDL_Integer)(log(Random(Seq)) / log(p)); } SDL_Integer Poisson( SDL_Real m, RandomControl Seq ) { SDL_Real U, Term, Sum; int Sample; #ifdef XTRACE if ( xShouldBeTraced(xRandomTraceLevel, (xPrsNode)0) ) xPrintString(" Poisson\n"); #endif #ifdef XOPERRORF if ( m < 0.0 ) { xSDLOpError("Poisson in sort RandomControl", "Illegal parameter"); return 0.0; } #endif Term = exp(-m); Sum = Term; Sample = 0; U = Random(Seq); while (U > Sum) { Sample = Sample + 1; Term = Term * m/Sample; #ifdef XOPERRORF if ( Sum+Term == Sum ) { xSDLOpError("Poisson in sort RandomControl", "Function run out of significant digits"); return Sample; } #endif Sum = Sum + Term; } return Sample; } SDL_Integer RandInt(SDL_Integer Low, SDL_Integer High, RandomControl Seq ) { SDL_Real U; #ifdef XTRACE if ( xShouldBeTraced(xRandomTraceLevel, (xPrsNode)0) ) xPrintString(" RandInt\n"); #endif #ifdef XOPERRORF if ( Low > High ) { xSDLOpError("RandInt in sort RandomControl", "Illegal parameter"); return 0; } #endif U = Random(Seq); return (SDL_Integer)(Low*(1.0-U) + High*U + U); } RandomControl DefineSeed( SDL_Integer Seed ) { RandomControl R; R = (RandomControl)xAlloc((xptrint)sizeof(unsigned long)); #ifdef XOPERRORF if ( (Seed > 32767) || (Seed < 1) || (Seed%2 == 0) ) { xSDLOpError("DefineSeed in sort RandomControl", "Illegal Seed value"); *R = 65537; return R; } #endif if ( Seed < 8192 ) *R = 65537*Seed + 8192; else *R = 65537*Seed; return R; } SDL_Integer Seed( RandomControl Seq ) { return RandInt(1, 16383, Seq)*2 + 1; } #ifdef XREADANDWRITEF char * yWri_RandomControl( void * Value) { static WriteBuf *buf = 0; if (*(void **)Value != (void *)0) { if (!buf) buf = WriteBuf_New(14); else WriteBuf_Clear(buf); xGenericWriteSort(buf, *(void **)Value, (tSDLTypeInfo *)&ySDL_SDL_Integer); return WriteBuf_Data(buf); } return "0"; } int yRead_RandomControl( void * Result ) { if (*(RandomControl *)Result == (RandomControl)0) *(RandomControl *)Result = (RandomControl)xAlloc((xptrint)sizeof(unsigned long)); return xRead_SDL_Integer(*(void **)Result); } #endif */ ENDNEWTYPE;