会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
让心灵去旅行
幸福是旅程,而不是终点
博客园
首页
新随笔
联系
订阅
管理
随机抽取不重复的数据
为了实现从一堆文件中随机抽取一定比例,且不重复的文件出来,我尝试了好几种算法,这种比较简单,也能达成我的要求。
为了实现从一堆文件中随机抽取一定比例,且不重复的文件出来,我尝试了好几种算法,这种比较简单,也能达成我的要求。废话少说,代码如下:
/**/
///
<summary>
///
功能: 随机抽取一定比率,且不重复的文件名
///
创建人: AppleGreen
///
创建时间: 2005-11-28
///
</summary>
///
<param name="aryFileName">
包含文件名的数组
</param>
///
<param name="dblRate">
抽取比例
</param>
///
<returns>
文件名字符串
</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;
//
比率超过1,按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();
}
posted @
2005-11-28 17:17
让心灵去旅行
阅读(
1145
) 评论(
1
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告