Last change
on this file was
16766,
checked in by westram, 7 years ago
|
|
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Author Date Id Revision
|
File size:
1.1 KB
|
Line | |
---|
1 | #include "frand.h" |
---|
2 | #include <math.h> |
---|
3 | #include <stdlib.h> |
---|
4 | |
---|
5 | static double randval() { |
---|
6 | // Liefert einen Zufallswert zwischen -0.5 und +0.5 |
---|
7 | double val = rand(); |
---|
8 | |
---|
9 | val /= RAND_MAX; |
---|
10 | val -= 0.5; |
---|
11 | |
---|
12 | assert(val>=-0.5); |
---|
13 | assert(val <= 0.5); |
---|
14 | |
---|
15 | return val; |
---|
16 | } |
---|
17 | static double lowfreqrandval(double *val, int teiler) { |
---|
18 | // Liefert einen niederfrequenten Zufallswert zwischen -0.5 und +0.5 |
---|
19 | double add = randval()/teiler; |
---|
20 | |
---|
21 | *val += add; |
---|
22 | if (*val<-0.5 || *val>0.5) *val -= 2*add; |
---|
23 | |
---|
24 | return *val; |
---|
25 | } |
---|
26 | Frand initFrand(double medium, double low, double high) { |
---|
27 | Frand f = (Frand)malloc(sizeof(*f)); |
---|
28 | |
---|
29 | if (!f) outOfMemory(); |
---|
30 | |
---|
31 | f->medium = medium; |
---|
32 | f->alpha = high*2; |
---|
33 | f->beta = low*2; |
---|
34 | f->val = randval(); |
---|
35 | f->teiler = 1; |
---|
36 | |
---|
37 | return f; |
---|
38 | } |
---|
39 | double getFrand(Frand f) { |
---|
40 | return f->medium + |
---|
41 | f->alpha * randval() + |
---|
42 | f->beta * lowfreqrandval(&(f->val), f->teiler); |
---|
43 | } |
---|
44 | void freeFrand(Frand f) { |
---|
45 | free(f); |
---|
46 | } |
---|
47 | double randProb() { |
---|
48 | // Liefert einen Zufallswert zwischen 0.0 und 1.0 |
---|
49 | double val = rand(); |
---|
50 | |
---|
51 | val /= RAND_MAX; |
---|
52 | |
---|
53 | assert(val>=0.0); |
---|
54 | assert(val<=1.0); |
---|
55 | |
---|
56 | return val; |
---|
57 | } |
---|
58 | |
---|
Note: See
TracBrowser
for help on using the repository browser.