Random.cpp
Go to the documentation of this file.
1
/*
2
* random.c
3
*
4
* implementiert lagged Fibonacci random sequence
5
* exakt nach Knuth TAOCP Vol.2 pp.186f
6
*
7
*/
8
#include "
Random.h
"
9
10
#include <stdio.h>
11
12
using namespace
MiscLib
;
13
14
#define KK 100
15
#define LL 37
16
#define MM MiscLib_RN_RAND_MOD
17
#define TT 70
18
19
#define mod_diff(x, y) ((x) - (y) & (MM - 1))
20
#define is_odd(x) ((x) & 1)
21
#define evenize(x) ((x) & (MM - 2))
22
23
size_t
MiscLib::rn_buf
[
MiscLib_RN_BUFSIZE
];
24
size_t
MiscLib::rn_point
=
MiscLib_RN_BUFSIZE
;
25
26
void
27
MiscLib::rn_setseed
(
size_t
seed)
28
{
29
register
int
t, j;
30
size_t
x
[
KK
+
KK
- 1];
31
register
size_t
ss =
evenize
(seed + 2);
32
for
(j = 0; j <
KK
; j++)
33
{
34
x
[j] = ss;
35
ss <<= 1;
36
if
(ss >=
MM
)
37
{
38
ss -=
MM
- 2;
39
}
40
}
41
for
(; j <
KK
+
KK
- 1; j++)
42
{
43
x
[j] = 0;
44
}
45
x
[1]++;
46
ss = seed & (
MM
- 1);
47
t =
TT
- 1;
48
while
(t)
49
{
50
for
(j =
KK
- 1; j > 0; j--)
51
{
52
x
[j + j] =
x
[j];
53
}
54
for
(j =
KK
+
KK
- 2; j >
KK
-
LL
; j -= 2)
55
{
56
x
[
KK
+
KK
- 1 - j] =
evenize
(
x
[j]);
57
}
58
for
(j =
KK
+
KK
- 2; j >=
KK
; j--)
59
if
(
is_odd
(
x
[j]))
60
{
61
x
[j - (
KK
-
LL
)] =
mod_diff
(
x
[j - (
KK
-
LL
)],
x
[j]);
62
x
[j -
KK
] =
mod_diff
(
x
[j -
KK
],
x
[j]);
63
}
64
if
(
is_odd
(ss))
65
{
66
for
(j =
KK
; j > 0; j--)
67
{
68
x
[j] =
x
[j - 1];
69
}
70
x
[0] =
x
[
KK
];
71
if
(
is_odd
(
x
[
KK
]))
72
{
73
x
[
LL
] =
mod_diff
(
x
[
LL
],
x
[
KK
]);
74
}
75
}
76
if
(ss)
77
{
78
ss >>= 1;
79
}
80
else
81
{
82
t--;
83
}
84
}
85
for
(j = 0; j <
LL
; j++)
86
{
87
rn_buf
[j +
KK
-
LL
] =
x
[j];
88
}
89
for
(; j <
KK
; j++)
90
{
91
rn_buf
[j -
LL
] =
x
[j];
92
}
93
}
94
95
size_t
96
MiscLib::rn_refresh
()
97
{
98
/* You remember Duff's device? If it would help then it should be used here */
99
rn_point
= 1;
100
101
register
int
i, j;
102
for
(j =
KK
; j <
MiscLib_RN_BUFSIZE
; j++)
103
{
104
rn_buf
[j] =
mod_diff
(
rn_buf
[j -
KK
],
rn_buf
[j -
LL
]);
105
}
106
for
(i = 0; i <
LL
; i++, j++)
107
{
108
rn_buf
[i] =
mod_diff
(
rn_buf
[j -
KK
],
rn_buf
[j -
LL
]);
109
}
110
for
(; i <
KK
; i++, j++)
111
{
112
rn_buf
[i] =
mod_diff
(
rn_buf
[j -
KK
],
rn_buf
[i -
LL
]);
113
}
114
115
116
return
*
rn_buf
;
117
}
LL
#define LL
Definition:
Random.cpp:15
MiscLib_RN_BUFSIZE
#define MiscLib_RN_BUFSIZE
Definition:
Random.h:10
MiscLib::rn_point
size_t rn_point
Definition:
Random.cpp:24
KK
#define KK
Definition:
Random.cpp:14
TT
#define TT
Definition:
Random.cpp:17
MiscLib::rn_setseed
void rn_setseed(size_t)
Definition:
Random.cpp:27
is_odd
#define is_odd(x)
Definition:
Random.cpp:20
MiscLib
Definition:
AlignedAllocator.h:12
MiscLib::rn_buf
size_t rn_buf[]
Definition:
Random.cpp:23
MiscLib::rn_refresh
size_t rn_refresh(void)
Definition:
Random.cpp:96
Random.h
evenize
#define evenize(x)
Definition:
Random.cpp:21
mod_diff
#define mod_diff(x, y)
Definition:
Random.cpp:19
MM
#define MM
Definition:
Random.cpp:16
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition:
ArmarXTimeserver.cpp:27
VisionX
components
pointcloud_processor
EfficientRANSACPrimitiveExtractor
EfficientRANSAC
MiscLib
Random.cpp
Generated by
1.8.17