我想在vb.net中生成一个6个随机数的随机数,这个数字应该是数字,有时也是alphanumerice.Like ist 4或5个数字应该是数字,接下来应该是alphnumeric.I分开创建两个数字。像这样
Public Function rand() As String
'Number
Dim rng As Random = New Random
Dim number As Integer = rng.Next(1, 1000000)
Dim digits As String = number.ToString("000000")
'Alphnumeric
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim r As New Random
Dim sb As New StringBuilder
For i As Integer = 1 To 5
Dim idx As Integer = r.Next(0, 35)
sb.Append(s.Substring(idx, 1))
Next
End Function
现在任何人都有这个想法,在这个函数中,我可以返回数字和一些字符串,或者可以是一个创建数字和字符串的单个实例。
答案 0 :(得分:1)
要解决的第一个问题是随机数生成器。您应该只使用一个实例,而不是多个实例,如果在短时间内调用,则会返回相同的序列。然后就是 很难说你的要求中有什么“东西”意味着什么,但假设你有70%的粗糙数字和30%的数字和字符串组合,那么你可以调用随机生成器来决定一系列只有数字或混合数字。基于从适当的字符串随机选择序列构建的输出
' A global unique random generator'
Dim rng As Random = New Random
Sub Main
Console.WriteLine(rand())
Console.WriteLine(rand())
Console.WriteLine(rand())
Console.WriteLine(rand())
End Sub
Public Function rand() As String
Dim sb As New StringBuilder
' Selection of pure numbers sequence or mixed one
Dim pureNumbers = rng.Next(1,11)
if pureNumbers < 7 then
' Generate a sequence of only digits
Dim number As Integer = rng.Next(1, 1000000)
Dim digits As String = number.ToString("000000")
For i As Integer = 1 To 6
Dim idx As Integer = rng.Next(0, digits.Length)
sb.Append(digits.Substring(idx, 1))
Next
else
' Generate a sequence of digits and letters
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
For i As Integer = 1 To 6
Dim idx As Integer = rng.Next(0, 36)
sb.Append(s.Substring(idx, 1))
Next
End if
return sb.ToString()
End Function