# Defining a RNG for random_shuffle

## Recommended Posts

I want to use random_shuffle to randomize a couple of vectors in my current program, but I want to be able to do the same shuffle twice, say on two parallel vectors. (Lots of reasons that I need to keep these two vectors beyond the scope of this message.)

To accomplish this, I've got this (among much other stuff) in my current class definition:

private:    ptrdiff_t myrandom (ptrdiff_t i) {return rand()%i; };    ptrdiff_t (*p_myrandom)(ptrdiff_t);

and this in my constructor:

p_myrandom = myrandom;

My assumption is that before I call random_shuffle, I can grab a number from rand() which I will use as the seed each time I call random_shuffle to ensure the same ordering. Something like this:

seed = rand() % 1000;srand(seed);random_shuffle(vector1.begin(), vector1.end(), p_myrandom);srand(seed);random_shuffle(vector2.begin(), vector2.end(), p_myrandom);

But, when i try to compile, I get this referring to the line in the second box, above:

Z:\Development\Applications\MyClass.cpp:25: error: argument of type ptrdiff_t (DataBase::)(ptrdiff_t)' does not match ptrdiff_t (*)(ptrdiff_t)'

What obvious thing am I doing wrong here?

-Kirk

##### Share on other sites
You are providing a member function instead of a stand-alone function.

##### Share on other sites
And that is not allowed? So it should be as simple as moving the function definition out of the class definition, but making it global, correct?

##### Share on other sites
myrandom is a member function, and you're not declaring p_myrandom to be a member function pointer, just a regular function pointer. If you make myrandom static you can store its address in p_myrandom.

##### Share on other sites
Quote:
 Original post by kirkdAnd that is not allowed? So it should be as simple as moving the function definition out of the class definition, but making it global, correct?

Or keep it in the class, but make it static. It doesn't actually require any data from a class instance to work, after all.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978705

• 21
• 14
• 12
• 10
• 12