Sign in to follow this  
kirkd

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by kirkd
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?


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this