Created
August 2, 2016 00:37
-
-
Save thoughtcroft/7528d2e18f054fe65b1846ae862c9f81 to your computer and use it in GitHub Desktop.
2008-06-24-password-generator.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Function GeneratePassword( _ | |
ByVal intLength As Integer) As String | |
' Generates a random string of digits of the requested length | |
' In: | |
' intLength - number of digits to be returned (max 9) | |
' Out: | |
' Return Value - a random string of digits | |
' Example: | |
' GetPassword(3) = "927" | |
Dim lngHighNumber As Long | |
Dim lngLowNumber As Long | |
Dim lngRndNumber As Long | |
' Check we don't exceed our maximum range | |
If intLength > 9 Or intLength < 1 Then | |
Err.Raise 5, "GetPassword", _ | |
"Invalid string length - must be between 1 and 9" | |
Else | |
' Work out the numbers | |
lngLowNumber = 10 ^ (intLength - 1) | |
lngHighNumber = (10 ^ intLength) - 1 | |
' Generate a new seed and a new random number | |
Randomize | |
lngRndNumber = Int((lngHighNumber - lngLowNumber + 1) * Rnd) + lngLowNumber | |
' Format the result as string | |
GeneratePassword = Format$(lngRndNumber, String$(intLength, "0")) | |
End If | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment