# Random password generator with algorithm

In this article we are going to create a random password generator, which will be highly flexible in terms of length of the password and the used characters set. The algorithm will be implemented in Scilab and also as an in-page application.

Passwords are critical for cyber security. A good password must have at least 8 random characters in length and should contain lower case letters, upper case letters, numbers and at least one special character.

For a greater understanding on how the random password generator works, let’s start from its logical diagram.

The character set definition means deciding which characters are going to go into the password. Since we are doing the password generator in Scilab, we are going to define 4 string variables, containing the characters. By changing the content of each variable, we can limit the usage of certain characters.

`lowCase = "abcdefghijklmnopqrstuvxyz";`
`upCase = "ABCDEFGHIJKLMNOPQRSTUVXYZ";`
`Numbers = "0123456789";`
`SpecialChar = "£\$&()*+[]@#^-_!?";`

For our example we are going to use all alphanumeric characters and a decent amount of special characters.

Next we’ll define a numeric variable which contains the total number of characters set. In our case it will be 4: lower case, upper case, numbers and special characters. The purpose of this variable is to randomly loop through the sets at each iteration of the password generation.

`charCategories = 4;`

Next, we’ll define another numerical variable which contains the length of the password. The longer the password, the higher the security. We’ll start from a length of 8 characters, which can be reduced to a minimum of 1 or to any maximum. Take care that if you enter a very high number of characters your computer will use a lot of computing resources.

`passLength = 8;`

Before we dive into the password generation loop, we’ll define an empty string variable, which will contain our password.

`password="";`

The main algorithm for password generation contains a FOR loop, which does the following:

• generates a random number between 1 and 4 to choose the character set
• once the set has been randomly selected, a random index will be generated for the selected character set
• the index is used to choose the new character to be added to the previous character

The FOR loop is executed until the password length is reached.

Finally the password is displayed in the Scilab console.

The complete Scilab instructions for password generation is displayed below.

```clear()
clc()

// Define character set
lowCase = "abcdefghijklmnopqrstuvxyz";
upCase = "ABCDEFGHIJKLMNOPQRSTUVXYZ";
Numbers = "0123456789";
SpecialChar = "£\$&()*+[]@#^-_!?";
charCategories = 4;

passLength = 8;

// FOR loop
for i=1:passLength
chooseCharGroup = round(abs(rand()*(charCategories-1)));
select chooseCharGroup
case 0
index = round(abs(rand()*(length(lowCase)-1)));
case 1
index = round(abs(rand()*(length(upCase)-1)));
case 2
index = round(abs(rand()*(length(Numbers)-1)));
case 3
index = round(abs(rand()*(length(SpecialChar)-1)));
end
end

// Display password in Scilab console
```

Let’s run the algorithm for a password length of 8 characters:

`Aj[1Iq0X`

We can also try it out for a password of 50 characters:

`F3@JV9i10oI5_J@\$h0tHO#Sc3B33+Iu£[1K0f945JaNvJ3q35)`

To verify that the algorithm works accordingly we are going to perform a test with a limited number of characters in each category.

`lowCase = "az";`
`upCase = "AZ";`
`Numbers = "09";`
`SpecialChar = "£?";`

This test will demonstrate that a character from each set is used and also that the first and last character from each set is chosen randomly. Running it for a 50 characters length password gives:

`ZzA00Z9aZ9Z9a9Z0?aA£a£A0AAAZz9099a99?09?9A0zA9A?9?`

The same algorithm is implemented in the generator below. The maximum password length is limited to 99 characters. You can tryout different combinations of character sets, to test its performance.

 Lower case characters Upper case characters Numbers Special characters Password length (click button above)

For more tutorials in Scilab, check the Scilab tutorials page.