Jump to content
  • Advertisement
Sign in to follow this  
kirkd

Defining a RNG for random_shuffle

This topic is 2851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!