Re: Using a different rng with std::random_shuffle
On 24-12-2010 5:04, ?? Tiib wrote:
I believe that you need there a function accepting int parameter and
returning random int in range [0,parameter). It may be trivial like:
int funcar( int x )
{
return rg.IRandom( 0, x );
}
Just an example, i don't really know your TRandomMersenne's interface.
Thank you, ?? Tiib.
I got things working after studying the random_shuffle example at
http://www.cplusplus.com/reference/algorithm/random_shuffle/
I used Rick Wagner's Mersenne software:
http://www-personal.umich.edu/~wagnerr/MersenneTwister.html
Here is the essential code:
#include <iostream>
#include "MersenneTwister.h"
#include <vector>
#include <algorithm>
using namespace std;
MTRand mtrand1;
// random generator function:
ptrdiff_t myrandom (ptrdiff_t i)
{
return mtrand1.randInt(i);
}
// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
int main()
{
// A Mersenne Twister random number generator
// can be declared with a simple
//MTRand mtrand1;
const int n = 10;
vector<int> vint(n);
for (int i = 0; i != n; ++i)
vint[i] = i+1;
random_shuffle (vint.begin(), vint.end(), p_myrandom);
for (int i = 0; i != n; ++i)
cout << vint[i] << ' ';
return 0;
}
T.