随机密码生成器问题 [英] Random password generator problem
本文介绍了随机密码生成器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好。我正在尝试编写随机密码生成器,但我认为有问题。
问题是:
Hello. I am trying to code a random password generator but I assume a problem.
Problem is:
str2 = Conversions.ToString(random.Next(0, pool.Length))
str = (str & Conversions.ToString(pool(Conversions.ToInteger(str2))))
我尝试过:
What I have tried:
Dim num As Integer = 0
Me.TextBox1.Text = ""
Dim str As String = ""
Dim random As New Random
Dim str2 As String = ""
Do While (num < 3)
str2 = Conversions.ToString(random.Next(0, pool.Length))
str = (str & Conversions.ToString(pool(Conversions.ToInteger(str2))))
num += 1
Loop
Me.TextBox1.Text = ("1618ML00" & str & "8")
推荐答案
你可能会想要真正描述问题。只是发布一行代码作为问题并没有真正说明你认为问题是什么。
此外,这行代码本身是一个问题,因为它的编写非常糟糕,以及它所使用的整个方法。
看起来你正在尝试使用随机索引生成随机密码到一个字符数组中,正确吗?
方法应该完成一件事。在这种情况下,生成由随机字符组成的字符串。它不应该对文本框或预先发布/附加任何字符而不是它所挑选的字符串。
You might want to actually describe the problem. Just posting a line of code as the problem doesn't really say anything about what you think the problem is.
Also, that line of code itself IS a problem because it's so badly written, along with the entire method it's in.
It looks like you're trying to generate a random password using random indexes into an array of characters, correct?
A method should do exactly ONE thing. In this case, generate a string made up of random characters. It should NOT do anything with a textbox or preprend/append any characters other than the ones it's picking to the string.
' Assume "pool" is an array of characters accessible by this method.
Public Function GeneratePasswordString(length As Integer) As String
If length < 0 Then
Throw New ArgumentException
End If
Dim RNG As New Random
Dim buffer As New StringBuilder
Dim index As Integer
For count As Integer = 1 to length
index = RNG.Next(0, pool.length)
buffer.Append(pool(index))
Next
Return buffer.ToString
End Function
这篇关于随机密码生成器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文