1 | #include "frand.h" |
---|
2 | |
---|
3 | #ifndef __DEFINES_H |
---|
4 | #include "defines.h" |
---|
5 | #endif |
---|
6 | #ifndef __MATH_H |
---|
7 | #include <math.h> |
---|
8 | #endif |
---|
9 | #ifndef __STDLIB_H |
---|
10 | #include <stdlib.h> |
---|
11 | #endif |
---|
12 | |
---|
13 | /* -------------------------------------------------------------------------- */ |
---|
14 | /* static double randval(void) */ |
---|
15 | /* ------------------------------------------------------ 16.05.95 20.17 ---- */ |
---|
16 | /* */ |
---|
17 | /* Liefert einen Zufallswert zwischen -0.5 und +0.5 */ |
---|
18 | /* */ |
---|
19 | static double randval(void) |
---|
20 | { |
---|
21 | double val = rand(); |
---|
22 | |
---|
23 | val /= RAND_MAX; |
---|
24 | val -= 0.5; |
---|
25 | |
---|
26 | assert(val>=-0.5); |
---|
27 | assert(val<= 0.5); |
---|
28 | |
---|
29 | return val; |
---|
30 | } |
---|
31 | /* -------------------------------------------------------------------------- */ |
---|
32 | /* static double lowfreqrandval(double *val) */ |
---|
33 | /* ------------------------------------------------------ 16.05.95 20.17 ---- */ |
---|
34 | /* */ |
---|
35 | /* Liefert einen niederfrequenten Zufallswert zwischen -0.5 und +0.5 */ |
---|
36 | /* */ |
---|
37 | static double lowfreqrandval(double *val, int teiler) |
---|
38 | { |
---|
39 | double add = randval()/teiler; |
---|
40 | |
---|
41 | *val += add; |
---|
42 | if (*val<-0.5 || *val>0.5) *val -= 2*add; |
---|
43 | |
---|
44 | return *val; |
---|
45 | } |
---|
46 | /* -------------------------------------------------------------------------- */ |
---|
47 | /* Frand initFrand(double medium, double low, double high) */ |
---|
48 | /* ------------------------------------------------------ 17.05.95 15:35 ---- */ |
---|
49 | Frand initFrand(double medium, double low, double high) |
---|
50 | { |
---|
51 | Frand f = (Frand)malloc(sizeof(*f)); |
---|
52 | |
---|
53 | if (!f) outOfMemory(); |
---|
54 | |
---|
55 | f->medium = medium; |
---|
56 | f->alpha = high*2; |
---|
57 | f->beta = low*2; |
---|
58 | f->val = randval(); |
---|
59 | f->teiler = 1; |
---|
60 | |
---|
61 | return f; |
---|
62 | } |
---|
63 | /* -------------------------------------------------------------------------- */ |
---|
64 | /* double getFrand(Frand f) */ |
---|
65 | /* ------------------------------------------------------ 17.05.95 15:35 ---- */ |
---|
66 | double getFrand(Frand f) |
---|
67 | { |
---|
68 | return f->medium + |
---|
69 | f->alpha * randval() + |
---|
70 | f->beta * lowfreqrandval(&(f->val), f->teiler); |
---|
71 | } |
---|
72 | /* -------------------------------------------------------------------------- */ |
---|
73 | /* void freeFrand(Frand f) */ |
---|
74 | /* ------------------------------------------------------ 17.05.95 15.36 ---- */ |
---|
75 | void freeFrand(Frand f) |
---|
76 | { |
---|
77 | free(f); |
---|
78 | } |
---|
79 | /* -------------------------------------------------------------------------- */ |
---|
80 | /* double randProb(void) */ |
---|
81 | /* ------------------------------------------------------ 16.05.95 20.17 ---- */ |
---|
82 | /* */ |
---|
83 | /* Liefert einen Zufallswert zwischen 0.0 und 1.0 */ |
---|
84 | /* */ |
---|
85 | double randProb(void) |
---|
86 | { |
---|
87 | double val = rand(); |
---|
88 | |
---|
89 | val /= RAND_MAX; |
---|
90 | |
---|
91 | assert(val>=0.0); |
---|
92 | assert(val<=1.0); |
---|
93 | |
---|
94 | return val; |
---|
95 | } |
---|
96 | |
---|