source: branches/profile/TREEGEN/frand.c

Last change on this file was 7811, checked in by westram, 13 years ago

merge from dev [7748] [7749] [7750]

  • comments (C→C++ style)
  • fixed umlauts in TREEGEN
  • 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
5static double randval()
6{
7    //  Liefert einen Zufallswert zwischen -0.5 und +0.5
8    double val = rand();
9
10    val /= RAND_MAX;
11    val -= 0.5;
12
13    assert(val>=-0.5);
14    assert(val <= 0.5);
15
16    return val;
17}
18static double lowfreqrandval(double *val, int teiler)
19{
20    //  Liefert einen niederfrequenten Zufallswert zwischen -0.5 und +0.5
21    double add = randval()/teiler;
22
23    *val += add;
24    if (*val<-0.5 || *val>0.5) *val -= 2*add;
25
26    return *val;
27}
28Frand initFrand(double medium, double low, double high)
29{
30    Frand f = (Frand)malloc(sizeof(*f));
31
32    if (!f) outOfMemory();
33
34    f->medium = medium;
35    f->alpha  = high*2;
36    f->beta   = low*2;
37    f->val    = randval();
38    f->teiler = 1;
39
40    return f;
41}
42double getFrand(Frand f)
43{
44    return f->medium +
45           f->alpha  * randval() +
46           f->beta   * lowfreqrandval(&(f->val), f->teiler);
47}
48void freeFrand(Frand f)
49{
50    free(f);
51}
52double randProb()
53{
54    //  Liefert einen Zufallswert zwischen 0.0 und 1.0
55    double val = rand();
56
57    val /= RAND_MAX;
58
59    assert(val>=0.0);
60    assert(val<=1.0);
61
62    return val;
63}
64
Note: See TracBrowser for help on using the repository browser.