DESERT 3.5.1
Loading...
Searching...
No Matches
uwrandomlib.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#include "uwrandomlib.h"
40
42{
43 for (int i = 0; i < GENER; i++) {
44 s1[i] = 1;
45 s2[i] = 123456789L;
46 iy[i] = 0;
47 }
48}
49void
50Uwrandomlib::SetSeed(long int s, int type)
51{
52 int j;
53 long int k;
54
55 if (s == 0)
56 s = 1;
57 s1[type] = s;
58 s2[type] = s;
59 for (j = NTAB + 7; j >= 0; j--) {
60 k = s1[type] / IQ1;
61 s1[type] = IA1 * (s1[type] - k * IQ1) - k * IR1;
62 if (s1[type] < 0)
63 s1[type] += IM1;
64 if (j < NTAB)
65 iv[type][j] = s1[type];
66 }
67 iy[type] = iv[type][0];
68}
69
70double
72{
73 int j;
74 long int k;
75 double t;
76
77 k = s1[type] / IQ1;
78 s1[type] = IA1 * (s1[type] - k * IQ1) - k * IR1;
79 if (s1[type] < 0)
80 s1[type] += IM1;
81
82 k = s2[type] / IQ2;
83 s2[type] = IA2 * (s2[type] - k * IQ2) - k * IR2;
84 if (s2[type] < 0)
85 s2[type] += IM2;
86
87 j = iy[type] / NDIV;
88 iy[type] = iv[type][j] - s2[type];
89 iv[type][j] = s1[type];
90 if (iy[type] < 1)
91 iy[type] += IMM1;
92 if ((t = AM * iy[type]) > RNMX)
93 return RNMX;
94 else
95 return t;
96}
97
98// Generate gaussian distributed numbers with mean m and stdev sigma
99double
100Uwrandomlib::Gauss(double m, double sigma, int type)
101{
102 static int cache = 0;
103 static float v1;
104 float v2, w, y;
105
106 if (cache) {
107 cache = 0;
108 return v1 * sigma + m;
109 } else {
110 do {
111 v1 = 2.0 * Rand01(type) - 1.0;
112 v2 = 2.0 * Rand01(type) - 1.0;
113 w = (v1 * v1) + (v2 * v2);
114 } while (w > 1.0);
115 y = sqrt((-2.0 * log(w)) / w);
116 v1 = v1 * y;
117 cache = 1;
118 return v2 * y * sigma + m;
119 }
120}
121
122// Generate Pareto distributed numbers
123// FX(x)=Prob(X<=x)
124// FX(x)=1-((beta+x)/beta)^-alpha x>=0
125// FX(x)=0 otherwise
126double
127Uwrandomlib::Pareto(double alpha, double beta, int type)
128{
129 return (beta * (pow((1. - Rand01(type)), alpha) - 1.));
130}
double Gauss(double m, double sigma, int type)
long int s1[GENER]
Definition uwrandomlib.h:68
void SetSeed(long int s, int type)
long int s2[GENER]
Definition uwrandomlib.h:69
long int iv[GENER][NTAB]
Definition uwrandomlib.h:71
long int iy[GENER]
Definition uwrandomlib.h:70
double Rand01(int type)
double Pareto(double alpha, double beta, int type)
Random function header.
#define NTAB
Definition uwrandomlib.h:53
#define IA2
Definition uwrandomlib.h:48
#define GENER
Definition uwrandomlib.h:41
#define IR2
Definition uwrandomlib.h:52
#define NDIV
Definition uwrandomlib.h:54
#define IA1
Definition uwrandomlib.h:47
#define IM1
Definition uwrandomlib.h:43
#define IR1
Definition uwrandomlib.h:51
#define IMM1
Definition uwrandomlib.h:46
#define IQ1
Definition uwrandomlib.h:49
#define RNMX
Definition uwrandomlib.h:56
#define IQ2
Definition uwrandomlib.h:50
#define AM
Definition uwrandomlib.h:45
#define IM2
Definition uwrandomlib.h:44