2015-04-28 66 views
1

我在vb中有這個函數,它似乎在生成8個字符的密碼之前似乎正在工作,但現在爲每個註冊到該網站的用戶生成相同的密碼。我不確定我出錯的地方,因爲我沒有弄錯這段代碼(代碼不是由我寫的)?任何幫助,將不勝感激。生成隨機8字符密碼的函數

function generatePassword() 
    Dim i, newPassword 
    newPassword = "" 
    For i = 1 to 3 
    newPassword = newPassword & Mid("ABCDEFGHJKLMNPQRSTUVWXYZ",randomInRange(1,24),1) 
    Next 
    For i = 4 to 7 
    newPassword = newPassword & Mid("23456789",randomInRange(1,8),1) 
    Next 
    'For i = 1 to 8 
    ' newPassword = newPassword & Mid("ABCDEFGHJKLMNPQRSTUVWXYZ23456789",randomInRange(1,32),1) 
    'Next 
    generatePassword = newPassword 
end function 

function randomInRange(lo,hi) 
    randomInRange =(Int((hi - lo + 1) * rnd + lo)) 
end function 

Dim newPassword = generatePassword() 

      Dim strSql As String = "INSERT INTO Student(" & _ 
          "StudentNo," & _ 
          "Surname," & _ 
          "FirstName," & _ 
          "MiddleName," & _ 
          "Gender," & _ 
          "Pathway," & _ 
          "[Level]," & _ 
          "QubEmail," & _ 
          "[Password]," & _ 
          "HomeEmail," & _ 
          "MobilePhone," & _ 
          "HomeTown," & _ 
          "PlacementYear," & _ 
          "Status," & _ 
          "DateEdited," & _ 
          "HomePhone) " & _ 
          "VALUES " & _ 
          "(" & _ 
          "'" & StudentNo.Text.Replace("'", "''") & "'," & _ 
          "'" & Surname.Text.Replace("'", "''") & "'," & _ 
          "'" & Forename.Text.Replace("'", "''") & "'," & _ 
          "'" & MiddleName.Text.Replace("'", "''") & "'," & _ 
          "'" & ddlGender.SelectedValue & "'," & _ 
          "'" & ddlPathway.SelectedValue & "'," & _ 
          "'" & ddlLevel.SelectedValue & "'," & _ 
          "'" & QUBEmail.Text.Replace("'", "''") & "'," & _ 
          "'" & newPassword & "'," & _ 
          "'" & HomeEmail.Text.Replace("'", "''") & "'," & _ 
          "'" & MobileNo.Text.Replace("'", "''") & "'," & _ 
          "'" & HomeTown.Text.Replace("'", "''") & "'," & _ 
          "" & PlacementYear & "," & _ 
          "'Seeking Placement'," & _ 
          " GETDATE() ," & _ 
          "'" & HomeNo.Text.Replace("'", "''") & "' " & _ 
          ")" 


      Dim addStudent As OleDbDataReader = Database.DoSQLReturnDataReader(strSql) 
      addStudent.Close() 

回答

1

似乎沒有任何代碼正在更新rnd變量。每次您撥打randomInRange以獲取不同的密碼時,都需要更新。

我建議你改變randomInRange完全讓它運行更乾淨。

這就是你需要做什麼:

Private rnd As Random = new Random() 
Function randomInRange(lo As Integer, hi As Integer) As Integer 
    Return rnd.Next(lo, hi + 1) 
End Function 

使這一變化產生的隨機密碼。

+1

完美,這工作。謝謝。 – mcclosa

-1

如果你自己不是程序員,你應該和編寫代碼的人交談。儘管如此,Rnd確實不是隨機的,而只是一個使用種子計算其值的高級數學公式。

該種子設置爲程序運行的時間。在沒有任何參與的情況下,種子將保持不變,這意味着所有的隨機數將實際上是相同的值;在每個generatePassword()處使用Randomize

Function generatePassword() 
    Randomize() 

    /.../ 
End Function 
+0

Randomize()調用與Random類沒有任何關係。它們是兩個完全無關的代碼。 – Enigmativity

+0

@Enigmativity我在想'Rnd()'函數。我的錯。 –

+0

@Enigmativity由於OP提供的代碼最初包含Rnd()函數,而不是Random類,所以我沒有看到你的投票或批評是如何被調用的。你應該考慮不要低估這個答案,因爲Randomize確實與RND函數有關。現在說到這裏,我仍然認爲Random類是一個更好的方法。 –