以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [讨论]用洗牌法抽取记录的例子是否有问题?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=86672)

--  作者:sjx71
--  发布时间:2016/6/22 21:22:00
--  [讨论]用洗牌法抽取记录的例子是否有问题?

在帮助的开发指南 》 Foxtable编程 》 杂谈 》高效随机抽取记录 中用洗牌法抽取记录的程序如下 


Dim cnt As Integer = DataTables("订单").DataRows.Count
Dim
ids1 As New List(of Integer) \'用于存储洗牌前的位置
Dim
ids2 As New List(of Integer) \'用于存储洗牌后的位置
For
i As Integer = 0 To cnt -1 \'准备初始的牌
    ids1.add(i)

Next
For
i As Integer = 0 To cnt - 1 \'开始洗牌
    ids2.Add(ids1(rand.Next(0,ids1.count)))

Next
Tables
("订单").StopRedraw()
DataTables
("订单").ReplaceFor("选择",False)
For
i As Integer = 0 To 100 - 1 \'100为要抽取的行数
   
DataTables("订单").DataRows(ids2(i))("选择") = True
Next
Tables
("订单").Filter = "[选择] = True"
Tables
("订单").ResumeRedraw()



我测试其中开始洗牌的程序有点问题,ids2中可能会有重复值

For i As Integer = 0 To cnt - 1 \'开始洗牌
    ids2.Add(ids1(rand.Next(0,ids1.count)))

Next


麻烦老师看看有问题么



--  作者:大红袍
--  发布时间:2016/6/22 21:56:00
--  

帮助文档这样写,有问题,洗牌,不是这样洗的。

 

Dim dt As DataTable = DataTables("表A")
Dim cnt As Integer = dt.DataRows.Count
Dim ids(cnt - 1) As Integer
For i As Integer = 0 To cnt -1
    ids(i) = i
Next
For i As Integer = 0 To cnt \\ 2 \'洗牌次数
    Dim id1 As Integer = rand.Next(0,cnt)
    Dim id2 As Integer = rand.Next(0,cnt)
    Dim vid As Integer = ids(id1)
    ids(id1) = ids(id2)
    ids(id2) = vid
Next
dt.StopRedraw()
For i As Integer = 0 To ids.length -1
    output.show(ids(i))
Next
dt.ResumeRedraw()