请检查下面我的代码:随机数不工作
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个字符串的不同序列,但它不能像那样工作。每次我得到相同的序列。
任何人都可以解释为什么它会这样做,并为这个问题的任何可能的解决方案?
注意:我已经在排队之前尝试了洗牌数据表,但仍然无效。
很难明白你正在尝试做的 –
我不是一个VB.net程序员,但我的预感是你不随机数种子设定成类似以毫秒为单位的当前时间。在计算机中没有真正的随机数字这样的东西,相反,它是一个数学函数,从这个种子开始给出一系列不相关的数字。如果您没有设置种子,它将默认为一个设定的数字,因此每次都会给出相同的序列。 – sradforth
别忘了,随机不是线程安全的。锁定它。 –