目前我有以下代码。它逐行读取文件db.lcz,并将每个结果作为项目放入数组中。
在db.lcz中,每行有一个10位数字,我想根据所述文件中的现有数字得到一个新的随机数。
-
任何想法都是受欢迎的,我已经尝试了一些选项,比如for循环等,但我一直迷路。我知道我必须针对概率方面检查每个字符值,但我不知道如何继续。感谢所有帮助。
谢谢!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lines() As String = IO.File.ReadAllLines("db.lcz")
Dim codeArray As New ArrayList()
For x As Integer = 0 To lines.GetUpperBound(0)
codeArray.Add(lines(x))
Next
For x = 0 To codeArray.Count - 1
MsgBox(codeArray.Item(x))
Next
Dim rng As New Random
Dim number As Integer = rng.Next(1, 1000000000) 'Test.GenerateDigits(rng, 9)
Dim digits As String = number.ToString("0000000000")
MsgBox(digits)
End Sub
Private Shared Function GenerateDigits(ByVal rng As Random, _
ByVal length As Integer) As String
Dim chArray As Char() = New Char(length - 1) {}
Dim i As Integer
For i = 0 To length - 1
chArray(i) = Convert.ToChar(rng.Next(10) + &H30)
Next i
Return New String(chArray)
End Function
答案 0 :(得分:0)
您可以使用shuffle函数随机重新排序数字,并使用索引数组。
s = "1234567890"
s1 = ""
ix = shuffle(s.Length - 1)
For i As Integer = 0 To s.Length - 1
s1 &= s(ix(i))
Next i
shuffle功能可以类似于:
Function shuffle(ByVal ncards As Integer) As List(Of Integer)
Dim rand As New Random
Dim ix As New List(Of Integer)
Dim k, n, tmp As Integer
For i as integer = 0 To ncards
ix.Add(i)
Next i
n = ncards
For i as integer = ncards To 1 Step -1
k = rand.Next(1, n + 1)
tmp = ix(i)
ix(i) = ix(k)
ix(k) = tmp
Next i
Return ix
End Function