Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created May 20, 2015 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaityo256/007fe738d297d606afb2 to your computer and use it in GitHub Desktop.
Save kaityo256/007fe738d297d606afb2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <stdlib.h>
//----------------------------------------------------------------------
bool called_srand = false;
//----------------------------------------------------------------------
int
myrand(const int max) {
if(called_srand){
called_srand = false;
return 0;
}
double r = static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0);
return static_cast<int>(r * max);
};
//----------------------------------------------------------------------
int
main(int argc, char **argv){
const int N = 10;
for(int i=0;i<10;i++){
int a[N] = {0,1,2,3,4,5,6,7,8,9};
called_srand = true;
std::random_shuffle(a,a+N,myrand);
for(int j=0;j<N;j++){
std::cout << a[j];
}
std::cout<< std::endl;
}
}
//----------------------------------------------------------------------
/*
$ g++ random_shuffle2.cc; ./a.out
8756219034
8425093176
5461082379
4065173982
3519607284
7014685293
2978013546
5209816473
7429816530
7053148962
$ clang++ random_shuffle2.cc; ./a.out
0138674592
0795126438
0725418693
0863741952
0328519647
0398146527
0358647129
0592438671
0954316872
0396124578
*/
//----------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment