Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-xt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ag-ohlberger
dune-community
dune-xt
Commits
44818615
Commit
44818615
authored
12 years ago
by
René Fritze
Browse files
Options
Downloads
Patches
Plain Diff
[common] introduce new random header with convience wrapper around std::random dists
parent
02c9cb49
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dune/stuff/common/random.hh
+122
-0
122 additions, 0 deletions
dune/stuff/common/random.hh
with
122 additions
and
0 deletions
dune/stuff/common/random.hh
0 → 100644
+
122
−
0
View file @
44818615
#ifndef DUNE_STUFF_RANDOM_HH
#define DUNE_STUFF_RANDOM_HH
#include
<random>
#include
<boost/assign/list_of.hpp>
namespace
Dune
{
namespace
Stuff
{
namespace
Common
{
namespace
Math
{
template
<
typename
T
,
bool
=
std
::
is_integral
<
T
>
::
value
>
struct
UniformDistributionSelector
{
};
template
<
typename
T
>
struct
UniformDistributionSelector
<
T
,
true
>
{
typedef
std
::
uniform_int_distribution
<
T
>
type
;
};
template
<
typename
T
>
struct
UniformDistributionSelector
<
T
,
false
>
{
typedef
std
::
uniform_real_distribution
<
T
>
type
;
};
template
<
class
T
,
class
DistributionImp
,
class
EngineImp
>
struct
RNG
{
typedef
DistributionImp
DistributionType
;
typedef
EngineImp
EngineType
;
EngineType
generator
;
DistributionType
distribution
;
RNG
(
EngineType
g
,
DistributionType
d
)
:
generator
(
g
)
,
distribution
(
d
)
{
}
T
operator
()()
{
return
distribution
(
generator
);
}
};
template
<
class
T
>
class
DefaultRNG
:
public
RNG
<
T
,
typename
UniformDistributionSelector
<
T
>::
type
,
std
::
default_random_engine
>
{
typedef
RNG
<
T
,
typename
UniformDistributionSelector
<
T
>::
type
,
std
::
default_random_engine
>
BaseType
;
public:
DefaultRNG
(
T
min
=
std
::
numeric_limits
<
T
>::
min
(),
T
max
=
std
::
numeric_limits
<
T
>::
max
())
:
BaseType
(
std
::
default_random_engine
(
std
::
random_device
()()),
typename
UniformDistributionSelector
<
T
>::
type
(
min
,
max
))
{
}
};
namespace
{
const
std
::
string
alphanums
(
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890"
);
const
std
::
string
other_printables
(
"!@#$%^&*()"
"`~-_=+[{]{
\\
|;:'
\"
,<.>/? "
);
}
class
RandomStrings
:
public
RNG
<
std
::
string
,
std
::
uniform_int_distribution
<
int
>
,
std
::
mt19937
>
{
typedef
RNG
<
std
::
string
,
std
::
uniform_int_distribution
<
int
>
,
std
::
mt19937
>
BaseType
;
const
int
length
;
public:
RandomStrings
(
int
l
)
:
BaseType
(
std
::
mt19937
(
std
::
random_device
()()),
std
::
uniform_int_distribution
<
int
>
(
0
,
alphanums
.
size
()
-
1
))
,
length
(
l
)
{
}
std
::
string
operator
()()
{
std
::
string
ret
(
length
,
'\0'
);
std
::
generate
(
std
::
begin
(
ret
),
std
::
end
(
ret
),
[
=
]()
{
return
alphanums
[
distribution
(
generator
)];
});
return
ret
;
}
};
}
// namespace Math
}
// namespace Common
}
// namespace Stuff
}
// namespace Dune
#endif // DUNE_STUFF_RANDOM_HH
/** Copyright (c) 2012, Rene Milk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the FreeBSD Project.
**/
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment