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