5楼洗牌的方法是错误的,会导致靠前的记录被抽取的概率大,下面这样,前后的机会才均等:
Dim cnt As Integer = DataTables("订单").DataRows.Count
Dim ids(cnt - 1) As Integer
Dim rnd(cnt -1) As Integer
For i As Integer = 0 To cnt -1
ids(i) = i
rnd(i) = rand.Next(0,cnt)
Next
array.sort(rnd,ids)
Tables("订单").StopRedraw()
DataTables("订单").ReplaceFor("选择",False)
For i As Integer = 1 To 100 '100为要抽取的行数
DataTables("订单").DataRows(ids(i-1))("选择") = True
Next
Tables("订单").Filter = "[选择] = True"
Tables("订单").ResumeRedraw()
但是Array.Sort帮助没有介绍,下面的代码完全采用帮助的知识,性能基本一致:
Dim cnt As Integer = DataTables("订单").DataRows.Count
Dim ids As New List(of Integer)
For i As Integer = 0 To cnt -1
ids.add(i)
Next
Do
Dim n As Integer = rand.Next(0,ids.count)
ids.RemoveAt(n)
Loop While ids.count > 100 '100为要随机抽取的行数
Tables("订单").StopRedraw()
DataTables("订单").ReplaceFor("选择",False)
For Each i As Integer In ids
DataTables("订单").DataRows(i)("选择") = True
Next
Tables("订单").Filter = "[选择] = True"
Tables("订单").ResumeRedraw()
[此贴子已经被作者于2015/5/14 23:14:15编辑过]