In order to randomly extract a certain percentage of files from a pile of files without repeated files, I tried several algorithms. This is simple and can meet my requirements. The Code is as follows:
/** // <Summary>
/// Function: Randomly extract a certain ratio of non-repeated file names
/// Created by applegreen
/// Creation Time: 2005-11-28
/// </Summary>
/// <Param name = "aryfilename"> array containing file names </param>
/// <Param name = "dblrate"> extraction ratio </param>
/// <Returns> file name string </returns>
Public static string getrandnum (arraylist aryfilename, double dblrate)
{
Int intfiletotal = aryfilename. count;
Double D = intfiletotal * dblrate;
Int intcount = int32.parse (D. tostring ());
If (intcount> intfiletotal) intcount = intfiletotal; // if the ratio is greater than 1, the value is 100%.
Random RDM = new random ();
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <intcount; I ++)
{
String strvalue = aryfilename [RDM. Next (1, intfiletotal-I)]. tostring ();
SB. append (strvalue + ";");
Aryfilename. Remove (strvalue );
}
Return sb. tostring ();
}