-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mh randutils #234
Draft
martinhoracek
wants to merge
2
commits into
devel
Choose a base branch
from
mh_randutils
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Mh randutils #234
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef DEVIATION_H | ||
#define DEVIATION_H | ||
|
||
#include <random> | ||
#include <cmath> | ||
|
||
/** | ||
* Implementation of some new distribution that can be | ||
* supplied to RandUtils | ||
* Fun fact: it should've been normal distribution, | ||
* but something went wrong | ||
*/ | ||
template< typename T > | ||
class SomeNewDistribution { | ||
public: | ||
|
||
SomeNewDistribution( T mean=0, T std=1 ) | ||
: mean( mean ) | ||
, std( std ) {} | ||
|
||
template< typename generator > | ||
T operator()( generator& gen ) { | ||
T u, v, x, y, q; | ||
do { | ||
u = uniform( gen ); | ||
v = 1.7156 * ( uniform( gen ) - 0.5 ); | ||
x = u - 0.449871; | ||
y = abs( v ) + 0.386595; | ||
q = x * x + y * ( 0.19600 * y - 0.25472 * x ); | ||
} while( q > 0.27597 && ( q > 0.27846 || v * v > -4. * std::log( u ) * u * u ) ); | ||
return mean + std * v / u; | ||
} | ||
|
||
private: | ||
|
||
T mean; | ||
T std; | ||
|
||
std::uniform_real_distribution<T> uniform; | ||
}; | ||
|
||
#endif //DEVIATION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <iostream> | ||
#include <random> | ||
|
||
#include "prng.h" | ||
#include "deviation.h" | ||
|
||
/** | ||
* Example program using new RandUtils | ||
*/ | ||
|
||
int main() { | ||
/** | ||
* You can supply own RNG generator | ||
*/ | ||
RandUtils<double, std::exponential_distribution<>, std::mt19937_64> exp_rand( 2.0 ); | ||
|
||
/** | ||
* Some distributions have predefined generators | ||
*/ | ||
ExponentialGenerator< double > eg( 2.0 ); | ||
GaussGenerator< double > gg( 2.0, 0.5 ); | ||
/** | ||
* You can use RandUtils with own deviation class | ||
*/ | ||
RandUtils< double, SomeNewDistribution< double > > exp_rand2; | ||
|
||
/** | ||
* reseed with own seed if determinism is needed | ||
*/ | ||
exp_rand.reseed( 1 ); | ||
|
||
double x = 0; | ||
|
||
for ( int i = 0; i < 100000; ++i ) { | ||
x += gg.rand(); | ||
} | ||
|
||
/** | ||
* if the generator is used only once, then just call static function | ||
*/ | ||
x += ExponentialGenerator< double >::rand_once( 2.0 ); | ||
|
||
std::cout << "x = " << x << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifndef PRNG_H | ||
#define PRNG_H | ||
|
||
#include <random> | ||
|
||
/** | ||
`distribution` must comply to https://en.cppreference.com/w/cpp/named_req/RandomNumberDistribution | ||
|
||
standard library distributions are also on that page | ||
*/ | ||
template< typename T, typename distribution, typename generator=std::mt19937 > | ||
class RandUtils { | ||
public: | ||
|
||
template< typename... Args > | ||
RandUtils( Args... args ) | ||
: distr( args... ) { | ||
gen.seed( rd() ); | ||
} | ||
|
||
/** | ||
* Reseeds generator with `seed` | ||
*/ | ||
void reseed( uint64_t seed ) { gen.seed( seed ); } | ||
/** | ||
* Uses random device to reseed generator | ||
*/ | ||
void reseed() { gen.seed( rd() ); } | ||
|
||
T rand() { | ||
return distr( gen ); | ||
} | ||
|
||
/** | ||
* Don't use this for generation of large amount of values !! | ||
*/ | ||
template< typename... Args > | ||
static T rand_once( Args... args ) { | ||
std::random_device rd; | ||
std::mt19937 gen( rd() ); | ||
|
||
return distribution( args... )( gen ); | ||
} | ||
|
||
private: | ||
std::random_device rd; | ||
generator gen; | ||
distribution distr; | ||
}; | ||
|
||
template< typename T > | ||
using GaussGenerator = RandUtils< T, std::normal_distribution<> >; | ||
|
||
template< typename T > | ||
using ExponentialGenerator = RandUtils< T, std::exponential_distribution<> >; | ||
|
||
#endif // PRNG_H |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a main in a library can cause a lot of troubles