Using a different rng with std::random_shuffle

T

Thomas

I hope someone more experienced than I am can help me with this. I'm
trying to let std::random_shuffle work with a different random number
generator (different from the built-in one). I am using a small library
based on the Mersenne Twister, with a header "randomc.h" and a source
file mersenne.cpp as include directives. I declared

int32 seed = time(0);
TRandomMersenne rg(seed);

outside main() to enable global scope. This works fine as far as
generating integers or doubles is concerned. As I said, I'd like to use
this rng with the random_shuffle algorithm. As I understand it, the
following code should do the job:

std::random_shuffle (vint.begin(), vint.end(), rg);

- but it doesn't. The third parameter should be a "predicate functor",
and apparently rg doesn't qualify as such. What am I doing wrong?

Thanks,
Thomas
 
Ö

Öö Tiib

I hope someone more experienced than I am can help me with this. I'm
trying to let std::random_shuffle work with a different random number
generator (different from the built-in one). I am using a small library
based on the Mersenne Twister, with a header "randomc.h" and a source
file mersenne.cpp as include directives. I declared

int32 seed = time(0);
TRandomMersenne rg(seed);
outside main() to enable global scope. This works fine as far as
generating integers or doubles is concerned. As I said, I'd like to use
this rng with the random_shuffle algorithm. As I understand it, the
following code should do the job:

std::random_shuffle (vint.begin(), vint.end(), rg);

- but it doesn't. The third parameter should be a "predicate functor",
and apparently rg doesn't qualify as such. What am I doing wrong?

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.
 
T

Thomas

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+1;
random_shuffle (vint.begin(), vint.end(), p_myrandom);
for (int i = 0; i != n; ++i)
cout << vint << ' ';

return 0;
}

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top