ARB
frand.c
Go to the documentation of this file.
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 
Definition: frand.h:8
double val
Definition: frand.h:9
static double randval()
Definition: frand.c:5
void add(int v)
Definition: ClustalV.cxx:461
double beta
Definition: frand.h:9
double randProb()
Definition: frand.c:47
double alpha
Definition: frand.h:9
void freeFrand(Frand f)
Definition: frand.c:44
Frand initFrand(double medium, double low, double high)
Definition: frand.c:26
double medium
Definition: frand.h:9
static double lowfreqrandval(double *val, int teiler)
Definition: frand.c:17
double getFrand(Frand f)
Definition: frand.c:39
struct S_Frand * Frand
#define assert(bed)
#define outOfMemory()
Definition: defines.h:14
int teiler
Definition: frand.h:13