Vb.net 多线程中的随机数不起作用
请检查我下面的代码:Vb.net 多线程中的随机数不起作用,vb.net,visual-studio-2010,random,datatable,Vb.net,Visual Studio 2010,Random,Datatable,请检查我下面的代码: Public Class tier1 Dim rnd As New System.Random() Function build1(ByVal dt As DataTable) As String Try For i = 0 To 4 For ix As Integer = 0 To till Step 4 lstrn.Add(rnd.Next(ix, ix + 4))
Public Class tier1
Dim rnd As New System.Random()
Function build1(ByVal dt As DataTable) As String
Try
For i = 0 To 4
For ix As Integer = 0 To till Step 4
lstrn.Add(rnd.Next(ix, ix + 4))
Next
Dim cntx As Integer = 0
For Each x As Integer In lstrn
If (i = 0) Then
If (article(x).Split(ChrW(10)).Length > 2) Then
If (article(x).Split(ChrW(10))(0).Length > 300) Then
first.Add(article(x).Split(ChrW(10))(0))
cntx = cntx + 1
If (cntx = 25) Then
Exit For
End If
End If
End If
End If
lstrn.Clear()
Next
Dim fi as String = "{"
For dx As Integer = 0 To first.Count - 2
fi = fi & w.spinl(first(dx), "light") & "|"
Next
fi = fi & "}"
Return fi
Catch ex As Exception
End Try
End Function
End Class
现在查看我的呼叫代码:
Dim w As WaitCallback = New WaitCallback(AddressOf beginscn)
For var As Integer = 1 To NumericUpDown1.Value
Dim param(1) As Object
param(0) = lst
param(1) = var
ThreadPool.QueueUserWorkItem(w, param)
Next
sub
sub beginscn()
Dim scntxt As String = t1.buildtier1(dt)
end sub
现在明白我付出了什么,我想要什么。假设我传递一个数据表,如下所示:
1,abcd,34,5
2,adfg,34,5
3,fhjrt,34,5
4,rtitk,34,5
我想要的是{abcd | adfg | fhjrt | rtitk},这个序列每次都应该是随机的。因为我传递了50-100个值,并在25处退出循环,所以每个输出都应该有一个不同的序列,由25个字符串组成,{|}格式,但它不是这样工作的。每次我都得到相同的序列
有人能解释为什么会这样,以及这个问题的任何可能的解决方案吗
注意:在对datatable进行排队之前,我已经尝试过对其进行洗牌,但它仍然不起作用。随机对象不是线程安全的。您可以通过在每个线程中创建随机对象的单独实例并使用线程ID生成 种子
很难理解您想做什么我不是VB.net程序员,但我的直觉是您没有将随机数种子设置为以毫秒为单位的当前时间。在计算机中没有真正的随机数,所以它是一个数学函数,它给出了一个从这个种子开始的不相关的数字序列。如果您不设置种子,它将默认为一个设置的数字,因此每次都会给您相同的序列。不要忘记,random不是线程安全的。在它周围放一个锁。
article()
数组中有什么?这就是问题所在。