随机抽取不重复的数据

为了实现从一堆文件中随机抽取一定比例,且不重复的文件出来,我尝试了好几种算法,这种比较简单,也能达成我的要求。废话少说,代码如下:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// 功能:        随机抽取一定比率,且不重复的文件名
InBlock.gif        
/// 创建人:        AppleGreen
InBlock.gif        
/// 创建时间:    2005-11-28
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="aryFileName">包含文件名的数组</param>
InBlock.gif        
/// <param name="dblRate">抽取比例</param>
ExpandedBlockEnd.gif        
/// <returns>文件名字符串</returns>

None.gif          public   static   string  GetRandNum(ArrayList aryFileName, double  dblRate)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {    
InBlock.gif            
int intFileTotal = aryFileName.Count;
InBlock.gif            
double d = intFileTotal*dblRate;
InBlock.gif            
int intCount = Int32.Parse(d.ToString());
InBlock.gif            
if(intCount>intFileTotal) intCount = intFileTotal; //比率超过1,按100%算
InBlock.gif

InBlock.gif            Random rdm 
= new Random();
InBlock.gif            StringBuilder sb 
= new StringBuilder();
InBlock.gif            
for(int i=0;i<intCount;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strValue = aryFileName[rdm.Next(1,intFileTotal-i)].ToString();
InBlock.gif                sb.Append(strValue
+";");
InBlock.gif                aryFileName.Remove(strValue);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return sb.ToString();
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/applegreen/archive/2005/11/28/286274.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值