Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
b436d840
Commit
b436d840
authored
Dec 18, 2011
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a function for generating Gaussian random numbers.
parent
83623fd8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
0 deletions
+56
-0
rand_kernel_1.h
dlib/rand/rand_kernel_1.h
+56
-0
No files found.
dlib/rand/rand_kernel_1.h
View file @
b436d840
...
...
@@ -44,6 +44,10 @@ namespace dlib
max_val
*=
0x1000000
;
max_val
+=
0xFFFFFF
;
max_val
+=
0
.
01
;
has_gaussian
=
false
;
next_gaussian
=
0
;
}
virtual
~
rand
(
...
...
@@ -56,6 +60,9 @@ namespace dlib
mt
.
seed
();
seed
.
clear
();
has_gaussian
=
false
;
next_gaussian
=
0
;
// prime the generator a bit
for
(
int
i
=
0
;
i
<
10000
;
++
i
)
mt
();
...
...
@@ -92,6 +99,10 @@ namespace dlib
// prime the generator a bit
for
(
int
i
=
0
;
i
<
10000
;
++
i
)
mt
();
has_gaussian
=
false
;
next_gaussian
=
0
;
}
unsigned
char
get_random_8bit_number
(
...
...
@@ -164,6 +175,35 @@ namespace dlib
}
}
double
get_random_gaussian
(
)
{
if
(
has_gaussian
)
{
has_gaussian
=
false
;
return
next_gaussian
;
}
double
x1
,
x2
,
w
;
const
double
rndmax
=
std
::
numeric_limits
<
dlib
::
uint32
>::
max
();
// Generate a pair of Gaussian random numbers using the Box-Muller transformation.
do
{
const
double
rnd1
=
get_random_32bit_number
()
/
rndmax
;
const
double
rnd2
=
get_random_32bit_number
()
/
rndmax
;
x1
=
2
.
0
*
rnd1
-
1
.
0
;
x2
=
2
.
0
*
rnd2
-
1
.
0
;
w
=
x1
*
x1
+
x2
*
x2
;
}
while
(
w
>=
1
.
0
);
w
=
std
::
sqrt
(
(
-
2
.
0
*
std
::
log
(
w
)
)
/
w
);
next_gaussian
=
x2
*
w
;
has_gaussian
=
true
;
return
x1
*
w
;
}
void
swap
(
rand
&
item
...
...
@@ -171,6 +211,8 @@ namespace dlib
{
exchange
(
mt
,
item
.
mt
);
exchange
(
seed
,
item
.
seed
);
exchange
(
has_gaussian
,
item
.
has_gaussian
);
exchange
(
next_gaussian
,
item
.
next_gaussian
);
}
friend
void
serialize
(
...
...
@@ -190,6 +232,8 @@ namespace dlib
double
max_val
;
bool
has_gaussian
;
double
next_gaussian
;
};
...
...
@@ -210,8 +254,13 @@ namespace dlib
std
::
ostream
&
out
)
{
int
version
=
1
;
serialize
(
version
,
out
);
serialize
(
item
.
mt
,
out
);
serialize
(
item
.
seed
,
out
);
serialize
(
item
.
has_gaussian
,
out
);
serialize
(
item
.
next_gaussian
,
out
);
}
inline
void
deserialize
(
...
...
@@ -219,8 +268,15 @@ namespace dlib
std
::
istream
&
in
)
{
int
version
;
deserialize
(
version
,
in
);
if
(
version
!=
1
)
throw
serialization_error
(
"Error deserializing object of type rand: unexpected version."
);
deserialize
(
item
.
mt
,
in
);
deserialize
(
item
.
seed
,
in
);
deserialize
(
item
.
has_gaussian
,
in
);
deserialize
(
item
.
next_gaussian
,
in
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment