Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created May 19, 2015 10:37
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/2d2652590fb0dbfe6800 to your computer and use it in GitHub Desktop.
Save kaityo256/2d2652590fb0dbfe6800 to your computer and use it in GitHub Desktop.
A problem on random_shuffle with rand() on clang++
#include <iostream>
#include <algorithm>
#include <vector>
#include <stdlib.h>
//----------------------------------------------------------------------
int
myrand(const int max) {
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};
srand(i);
std::random_shuffle(a,a+N,myrand);
for(int j=0;j<N;j++){
std::cout << a[j];
}
std::cout<< std::endl;
}
}
//----------------------------------------------------------------------
/*
$ g++ random_shuffle.cc; ./a.out
9347128065
7645139208
7039421568
9705623418
5701438962
6709231458
9574203618
1476538902
3518960472
8917526340
$ clang++ random_shuffle.cc; ./a.out
2150497386
0286735941
0369472851
0415786239
0528496173
0685731249
0861527439
0942875163
0127583469
0283197645
*/
//----------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment