Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/build/pkgs/cddlib/patches/random.c
8818 views
1
/* Copyright (C) 1991, 1996 Free Software Foundation, Inc.
2
3
----------------------------------------------------------
4
Random numbers that return the same sequence on all platforms.
5
Implementation taken from the GNU C Library. The same copyright
6
applies.
7
----------------------------------------------------------
8
9
The GNU C Library is free software; you can redistribute it and/or
10
modify it under the terms of the GNU Lesser General Public
11
License as published by the Free Software Foundation; either
12
version 2.1 of the License, or (at your option) any later version.
13
14
The GNU C Library is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
Lesser General Public License for more details.
18
19
You should have received a copy of the GNU Lesser General Public
20
License along with the GNU C Library; if not, write to the Free
21
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22
02111-1307 USA. */
23
24
25
#include <stdint.h>
26
#include "random.h"
27
28
29
/* x**31 + x**3 + 1. */
30
#define TYPE_3 3
31
#define BREAK_3 128
32
#define DEG_3 31
33
#define SEP_3 3
34
35
36
static int32_t randtbl[DEG_3 + 1] =
37
{
38
TYPE_3,
39
40
-1726662223, 379960547, 1735697613, 1040273694, 1313901226,
41
1627687941, -179304937, -2073333483, 1780058412, -1989503057,
42
-615974602, 344556628, 939512070, -1249116260, 1507946756,
43
-812545463, 154635395, 1388815473, -1926676823, 525320961,
44
-1009028674, 968117788, -123449607, 1284210865, 435012392,
45
-2017506339, -911064859, -370259173, 1132637927, 1398500161,
46
-205601318,
47
};
48
49
50
struct random_data
51
{
52
int32_t *fptr; /* Front pointer. */
53
int32_t *rptr; /* Rear pointer. */
54
int32_t *state; /* Array of state values. */
55
int rand_type; /* Type of random number generator. */
56
int rand_deg; /* Degree of random number generator. */
57
int rand_sep; /* Distance between front and rear. */
58
int32_t *end_ptr; /* Pointer behind state table. */
59
};
60
61
static struct random_data rng_state =
62
{
63
.fptr = &randtbl[SEP_3 + 1],
64
.rptr = &randtbl[1],
65
.state = &randtbl[1],
66
.rand_type = TYPE_3,
67
.rand_deg = DEG_3,
68
.rand_sep = SEP_3,
69
.end_ptr = &randtbl[sizeof (randtbl) / sizeof (randtbl[0])]
70
};
71
72
73
void portable_srand(unsigned int seed)
74
{
75
int type;
76
int32_t *state;
77
long int i;
78
int32_t word;
79
int32_t *dst;
80
int kc;
81
82
/* printf("portable_srand: seed = %u\n", seed); */
83
84
type = rng_state.rand_type;
85
86
state = rng_state.state;
87
/* We must make sure the seed is not 0. Take arbitrarily 1 in this case. */
88
if (seed == 0)
89
seed = 1;
90
state[0] = seed;
91
92
dst = state;
93
word = seed;
94
kc = rng_state.rand_deg;
95
for (i = 1; i < kc; ++i)
96
{
97
/* This does:
98
state[i] = (16807 * state[i - 1]) % 2147483647;
99
but avoids overflowing 31 bits. */
100
long int hi = word / 127773;
101
long int lo = word % 127773;
102
word = 16807 * lo - 2836 * hi;
103
if (word < 0)
104
word += 2147483647;
105
*++dst = word;
106
}
107
108
rng_state.fptr = &state[rng_state.rand_sep];
109
rng_state.rptr = &state[0];
110
kc *= 10;
111
while (--kc >= 0)
112
portable_rand();
113
}
114
115
116
int portable_rand(void)
117
{
118
int32_t *fptr = rng_state.fptr;
119
int32_t *rptr = rng_state.rptr;
120
int32_t *end_ptr = rng_state.end_ptr;
121
int32_t result;
122
123
result = *fptr += *rptr;
124
/* Chucking least random bit. */
125
result = (result >> 1) & 0x7fffffff;
126
++fptr;
127
if (fptr >= end_ptr)
128
{
129
fptr = rng_state.state;
130
++rptr;
131
}
132
else
133
{
134
++rptr;
135
if (rptr >= end_ptr)
136
rptr = rng_state.state;
137
}
138
rng_state.fptr = fptr;
139
rng_state.rptr = rptr;
140
141
/* printf("portable_rand: output = %d\n", result); */
142
143
return result;
144
}
145
146