It was me DIO
It was me DIO

Reputation: 3

How to loop a sub w/ a variable value?

I'm creating a program that outputs a randomly generated password in which the user has jurasdiction over how many characters they want their password to be. How would i go about taking the input from the user and then looping the subroutine (which generates a single random character)? Any help would be much appreciated, thank you!

' this is the code for my input where n = the no. of characters the user wants in their pasword
Public Sub inp()
    Console.WriteLine("How many characters do you want in your password? 
    (up to 20 allowed)")
    n = Console.ReadLine()
End Sub

'this is the simple sub that generates a random number from which a character from my database is chosen
Public Sub randi()
    Randomize()
    i = Rnd() * 92
End Sub

Upvotes: 0

Views: 166

Answers (1)

Mary
Mary

Reputation: 15101

Let us think about the steps envolved.

  1. Ask user for a number
  2. Place the response in a variable.
  3. Create a random string with n characters

I looks like you have taken care of 1 and 2. Now let's break down number 3 into tasks. What characters do you want to include. Uppercase letters? Lowercase letters? Numbers? Punctuation? An ASCII table will tell you that there are readable characters from ASCII 33 to 126. If you were to ask for a random number in the range 33 to 126 then change it to the character it represnts, it would be a start.

Luckily .net provides a Random class. First we will need to get an instance of that class to use its methods. Private rnd As New Random Now we have a variable holding an instance of the random class so we can use any of the classed properties and methods. There is a method, .Next(Int32, Int32), that will produce a random number in a range. The first parameter is the bottom number in the range and the second is one more than the top number. Going back to ASCII table we need a random number like this rnd.Next(33, 125)

Next we need to change the result to a character. Convert.ToChar(Ascii number)

The next problem is to get the number of characters the user asked for. Remember n. We can do this is a For...Next loop.

Inside the loop we want to build a string with these random characters. This could be done with an ampersand equals but every time you changes a string the compiler has to throw away the old string and create an entirely new one because strings are immutable. To solve this .net has provided a StringBuilder class. We create an instance of this class and then use the .Append method to add the characters to the sb.

After the loop we change the StringBuilder to a regular string and return it to the calling code.

Public Sub Main()
    Console.WriteLine("How many characters do you want in your password? 
(up to 20 allowed)")
    Dim n As Integer = CInt(Console.ReadLine())
    Dim Pword As String = GetRandomString(n)
    Console.WriteLine(Pword)
End Sub

Private rnd As New Random

Private Function GetRandomString(stringLength As Integer) As String
    Dim sb As New StringBuilder
    For c = 1 To stringLength 
        Dim newAscii = rnd.Next(33, 127)
        Dim newChr = Convert.ToChar(newAscii)
        sb.Append(newChr)
    Next
    Return sb.ToString
End Function

In a real application you would have to check if the user entered a valid number. An Integer.TryParse and a check for 0 would probably do it.

Upvotes: 1

Related Questions