LibRan  0.1
Pseudo-random number distribution generator
LRgeom.c
Go to the documentation of this file.
1 
47 /*
48  * Copyright 2019 R.K. Owen, Ph.D.
49  * License see lgpl.md (Gnu Lesser General Public License)
50  */
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 #include <math.h>
56 #include "libran.h"
57 
58 /* int */
69  float zero = 0.0, one = 1.0, u, p = one;
70  if (isnan(o->q))
71  o->q = one/logf(one - o->p);
72 
73  do {
74  u = o->uf(o);
75  } while (u == zero);
76 
77  return ceilf(o->q * logf(u));
78 }
79 
88 float LRi_geometric_PDF(LR_obj *o, int x) {
89  float zero = 0.0, one = 1.0, p = o->p;
90 
91  if (x <= 0) return zero;
92 
93  if (x == 1) {
94  return p;
95  } else {
96  float q = one - p;
97  for (int nn = 2; nn <= x; nn++) {
98  p *= q;
99  }
100  return p;
101  }
102 }
103 
112 float LRi_geometric_CDF(LR_obj *o, int x) {
113  float zero = 0.0, one = 1.0, q = one - o->p, p = q;
114 
115  if (x <= 0) return zero;
116 
117  if (x == 1) {
118  return one - p;
119  } else {
120  for (int nn = 2; nn <= x; nn++) {
121  p *= q;
122  }
123  return one - p;
124  }
125 }
126 
127 #ifdef __cplusplus
128 }
129 #endif
float LRi_geometric_CDF(LR_obj *o, int x)
LRi_geometric_CDF(LR_obj *o, int x) - Geometric distribution cumulative distribution function...
Definition: LRgeom.c:112
float p
Definition: libran.h:145
float LRi_geometric_PDF(LR_obj *o, int x)
LRi_geometric_PDF(LR_obj *o, int x) - Geometric probablity (or mass) distribution function...
Definition: LRgeom.c:88
int LRi_geometric_RAN(LR_obj *o)
LRi_geometric_RAN(LR_obj *o) - int Geometric distributed variate. Default values: probability of succ...
Definition: LRgeom.c:68
float q
Definition: libran.h:146
float(* uf)(LR_obj *)
Definition: libran.h:153
The LibRan common header file.
the fundamental LibRan random variate distribution object
Definition: libran.h:134