我在函数中创建referenceid并将值添加到列表中。
Dim myListOfItems As New List(Of BasketItem)
Dim refID As String = String.Empty
For Each i In myListOfNames
refID = HelpClass.GenerateRandomString(20)
Dim x As New BasketItem
x.RefID = refID
myListOfItems.Add(x)
Next
函数如下所示:
Public Shared Function GenerateRandomString(ByVal length As Integer) As String
Dim chara As Char() = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray()
Dim randomString As String = String.Empty
Dim objRandom As New Random()
For i As Integer = 0 To length Step 1
Dim x As Integer = objRandom.Next(1, chara.Length)
If Not randomString.Contains(chara.GetValue(x).ToString()) Then
randomString += chara.GetValue(x)
Else
i = i - 1
End If
Next
Return randomString
End Function
这在我本地的visual studio运行中都很有效。但是当我上传到我的web服务器的几个项目得到相同的值。
这是在运行服务器上的输出:
> Biljettinnehavare: 1 | 1XIh4YqBlHmipkPKV576C
> Biljettinnehavare: 2 | 1XIh4YqBlHmipkPKV576C
> Biljettinnehavare: 3 | 1XIh4YqBlHmipkPKV576C
> Biljettinnehavare: 4 | 1XIh4YqBlHmipkPKV576C
> Biljettinnehavare: 5 | qvmupeZhcoQ9YgOWtCLN4
> Biljettinnehavare: 6 | qvmupeZhcoQ9YgOWtCLN4
> Biljettinnehavare: 7 | qvmupeZhcoQ9YgOWtCLN4
> Biljettinnehavare: 8 | qvmupeZhcoQ9YgOWtCLN4
> Biljettinnehavare: 9 | qvmupeZhcoQ9YgOWtCLN4
> Biljettinnehavare: 10 | qvmupeZhcoQ9YgOWtCLN4
> Biljettinnehavare: 11 | DdxK4jibcu9s7gXJw6a3m
> Biljettinnehavare: 12 | DdxK4jibcu9s7gXJw6a3m
> Biljettinnehavare: 13 | DdxK4jibcu9s7gXJw6a3m
> Biljettinnehavare: 14 | DdxK4jibcu9s7gXJw6a3m
> Biljettinnehavare: 15 | 32SWpkFfdgbqMtJGa1siw
> Biljettinnehavare: 16 | 32SWpkFfdgbqMtJGa1siw
我真的不知道是什么引起了这个问题。什么好主意吗?服务器执行循环如此之快,并基于时钟使相同的值出现,在这种情况下,如何应对?
将变量定义为:
Static objRandom As System.Random = New System.Random()
在VB中的答案随机整数。净