Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Matthias Mayr
limbo
Commits
f2f294d6
Commit
f2f294d6
authored
Jul 17, 2018
by
Konstantinos Chatzilygeroudis
Browse files
Provide more configurable random generator
parent
d78cd81c
Changes
1
Show whitespace changes
Inline
Side-by-side
src/limbo/tools/random_generator.hpp
View file @
f2f294d6
...
@@ -69,12 +69,22 @@ namespace limbo {
...
@@ -69,12 +69,22 @@ namespace limbo {
class
RandomGenerator
{
class
RandomGenerator
{
public:
public:
using
result_type
=
typename
D
::
result_type
;
using
result_type
=
typename
D
::
result_type
;
RandomGenerator
(
result_type
a
,
result_type
b
)
:
_dist
(
a
,
b
),
_rgen
(
randutils
::
auto_seed_128
{}.
base
())
{}
RandomGenerator
(
result_type
a
,
result_type
b
,
int
seed
=
-
1
)
:
_dist
(
a
,
b
)
{
this
->
seed
(
seed
);
}
result_type
rand
()
result_type
rand
()
{
return
_dist
(
_rgen
);
}
void
seed
(
int
seed
=
-
1
)
{
{
return
_dist
(
_rgen
);
if
(
seed
>=
0
)
_rgen
.
seed
(
seed
);
else
_rgen
.
seed
(
randutils
::
auto_seed_128
{}.
base
());
}
}
void
reset
()
{
_dist
.
reset
();
}
void
param
(
const
typename
D
::
param_type
&
param
)
{
_dist
.
param
(
param
);
}
private:
private:
D
_dist
;
D
_dist
;
std
::
mt19937
_rgen
;
std
::
mt19937
_rgen
;
...
@@ -95,36 +105,41 @@ namespace limbo {
...
@@ -95,36 +105,41 @@ namespace limbo {
/// Double random number generator (gaussian)
/// Double random number generator (gaussian)
using
rgen_gauss_t
=
RandomGenerator
<
rdist_gauss_t
>
;
using
rgen_gauss_t
=
RandomGenerator
<
rdist_gauss_t
>
;
///@ingroup tools
///
@ingroup tools
///integer random number generator
///
integer random number generator
using
rgen_int_t
=
RandomGenerator
<
rdist_int_t
>
;
using
rgen_int_t
=
RandomGenerator
<
rdist_int_t
>
;
/// @ingroup tools
/// @ingroup tools
/// random vector in [0, 1.0]
/// random vector by providing custom RandomGenerator
template
<
typename
Rng
>
inline
Eigen
::
VectorXd
random_vec
(
int
size
,
Rng
&
rng
)
{
Eigen
::
VectorXd
res
(
size
);
for
(
int
i
=
0
;
i
<
size
;
++
i
)
res
[
i
]
=
rng
.
rand
();
return
res
;
}
/// @ingroup tools
/// random vector in [0, 1]
///
///
/// - this function is thread safe because we use a random generator for each thread
/// - this function is thread safe because we use a random generator for each thread
/// - we use a C++11 random number generator
/// - we use a C++11 random number generator
inline
Eigen
::
VectorXd
random_vector_bounded
(
int
size
)
inline
Eigen
::
VectorXd
random_vector_bounded
(
int
size
)
{
{
static
thread_local
rgen_double_t
rgen
(
0.0
,
1.0
);
static
thread_local
rgen_double_t
rgen
(
0.0
,
1.0
);
Eigen
::
VectorXd
res
(
size
);
return
random_vec
(
size
,
rgen
);
for
(
int
i
=
0
;
i
<
size
;
++
i
)
res
[
i
]
=
rgen
.
rand
();
return
res
;
}
}
/// @ingroup tools
/// @ingroup tools
/// random vector generated with a normal distribution centered on 0, with standard deviation of 10
.0
/// random vector generated with a normal distribution centered on 0, with standard deviation of 10
///
///
/// - this function is thread safe because we use a random generator for each thread
/// - this function is thread safe because we use a random generator for each thread
/// - we use a C++11 random number generator
/// - we use a C++11 random number generator
inline
Eigen
::
VectorXd
random_vector_unbounded
(
int
size
)
inline
Eigen
::
VectorXd
random_vector_unbounded
(
int
size
)
{
{
static
thread_local
rgen_gauss_t
rgen
(
0.0
,
10.0
);
static
thread_local
rgen_gauss_t
rgen
(
0.0
,
10.0
);
Eigen
::
VectorXd
res
(
size
);
return
random_vec
(
size
,
rgen
);
for
(
int
i
=
0
;
i
<
size
;
++
i
)
res
[
i
]
=
rgen
.
rand
();
return
res
;
}
}
/// @ingroup tools
/// @ingroup tools
...
...
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