-
Notifications
You must be signed in to change notification settings - Fork 0
/
myrand.cpp
91 lines (66 loc) · 2.09 KB
/
myrand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/***************************************************************************
myrand.cc - description
-------------------
begin : Sep 24 2001
copyright : (C) 2001 by Tian-Li Yu
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include "myrand.h"
#ifdef PI
#undef PI
#endif
#define PI 3.14159265
#define N 624
void initRand () {
unsigned long init_key[N];
srand ((unsigned long) time (NULL));
for (int i = 0; i < N; i++)
init_key[i] = (unsigned long) time (NULL) * rand ();
init_by_array (init_key, N);
}
bool flip (double prob)
{
return (uniform () < prob);
}
/** From [0,1) */
double uniform ()
{
return genrand_res53 ();
}
/** From [a,b) */
double uniform (double a, double b)
{
return uniform () * (b - a) + a;
}
/** Int From [a,b] */
int uniformInt (int a, int b)
{
return (a + (int) (uniform () * (b - a + 1)));
}
/** Generate a random array of size num, from [a,b] */
/** num <= b-a+1 */
void uniformArray (int *array, int num, int a, int b)
{
int *base = new int[b - a + 1];
int i;
int r;
for (i = 0; i < b - a + 1; i++)
base[i] = a + i;
for (i = 0; i < num; i++) {
r = uniformInt (0, b - a - i);
array[i] = base[r];
base[r] = base[b - a - i];
}
delete[]base;
}