Randomly extract non-duplicate data

Source: Internet
Author: User

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.