samedi 7 février 2015

Generate k unique random numbers in the interval [0,n-1]?

How can I generate k unique random numbers in the interval [0,n-1]?


I used the following code:



for( int i = 0 ; i < n ; ++i ){
a[i]=i;
}

std::random_shuffle( a, a+n ) ;
for(int i=0;i<k;++i){
ra[i]=a[i];
}


which takes first k elements.


Can anyone refer me to a faster approach?


std::random_shuffle: http://ift.tt/1A4wqN9





Aucun commentaire:

Enregistrer un commentaire