Dec 29, 2008

Generate m random numbers from n numbers

Generate m random numbers from n numbers

The prob. of first number is chosen is m/n
If the first number is not chosen, the prob. of second number is m/(n-1);
Otherwise the prob. of second number is m-1/(n-1)
……

{{{
for(int i=0;i<n;i++)
{
if(rand()%(n-i)<m)
{
cout<<i<<endl;
m--;
}
}
}}}

No comments:

Post a Comment