Shuffle vector in C++


This question already has an answer here:


If srand(unsigned(time(NULL))); doesn't help, then your implementation must not be using the standard rand() as its random number generator. In that case, there is an alternative version of random_shuffle that takes a custom random number generator. You could just pass a wrapper around rand(), like this:

int MyRand(int n)
    return std::rand() % n;

// ...
std::random_shuffle(bar.begin(), bar.end(), MyRand);
// ...

If you want something with a more uniform distribution, look into the C++11 <random> header.

Well the implementation you have give cannot work (you named a type foo and used the same literal as a variable). Anyways try something like this

// random generator function:
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;

int main(void){
  random_shuffle(gameTiles.begin(), gameTiles.end(), p_myrandom);

It will ensure random_shuffle uses standard rand and that it is initialized properly. It should give different results unless you execute the application twice in the same second, that would lead to using the same seed for rand. The code it right from here but there is anything to change, so i just used it.

Need Your Help

JavaFx :Default Message for Empty ListView

java listview javafx javafx-2 javafx-8

When there is no record in any table it shows a message 'No content in table', which is by default functionality of TableView in JavaFx.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.