How To Ideas | How To Articles | How To Tutorials


1

How To Create Random Password Generator In C#


In today’s article, I will just show you how to create a very simple but useful application to create Random Password Generator in C#. I will be using Random class present in .NET to generate random numbers which then i will use to generate random character based on that number. You need not to be expertise in C# to create this application as in this application I will be using only these classes : Random , Int32 , Convert, Math.

Instructions:

  1. The main concept to create this application is converting a umber to its ASCII value character. As for every number, there exists a char value as ASCII value. So, if you convert any Int value to Char value, it will give you the corresponding ASCII value of that Integer as a character.
  2. So, you just have to generate random numbers actually and just convert them to their corresponding char value which you can then add in a string variable to get a Random Password.
  3. Here is what we get while we convert an integer to character, for 48 to 57, we will get value 0 to 9 in character, for 65 to 90, we will get value A to Z and for 97 to 122, we will get a to z.
  4. So, if you want to generate simple random password, with all the numbers or characters(either in upper case or lower case), then you can try the following code.

    int number = 8; // Length Of Your Password
    string password = "";
    for (int i = 0; i < number; i++)
    {
        password += Convert.ToString(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65))));
    }
    MessageBox.Show(password);

    Output Of Random Password

  5. Upper code will give you random password of 8 characters, and will only contains uppercase letters. To generate lowercase ones, replace 65 with 97. and if you want to generate password of random numbers, then replace 26 with 10, and 65 with 48.
  6. Now i will explain that big statement starting from the innermost expression. rnd.NextDouble() will give us a random double value ranging from 0 to 1 but neither 0 not 1, then i have multiplied that to 26(number of alphabets we have) which will give me any value ranging from 0 to 26 and then I have added that to the 65(starting value for A). So, final result of this expression will be 65 – 91.
  7. Now this result is in double value, but we need to have it in Integer format, so we have to convert it into an Integer value. That’s why I have used Convert.ToInt32() function. which will round off the value to the nearest integer.
  8. Now we have English alphabets ranging from 65 (A) to 90 (Z), So, we have to eliminate 91 which we will get as an integer if we convert any value ranging in between 90.5 to 91. For this I have used Math.Floor() function which will give us value from 65 to 90 including these two.
  9. Then I have converted the result into a Char value and finally by converting the Char value to String, I have concatenated that to password (String) variable.
  10. Using this scheme, you can get a random password of any type you want.

You can also download my sample code, in which you can get password of any type i.e Integers, Characters(either Uppercase or Lowercase), or a mixed password. Click Here To Download.

You can download one click start application from here.
Random Password generator

Incoming search terms:

  • code for random number generator in asp net using c# (1)
  • make generator c# (1)
Filed in: C# Tags: , , , , , , ,

One Response to "How To Create Random Password Generator In C#"

  1. Ajendra Prasad says:

    Nice example and can be used with some complexity for a working project…. thanks friend :)

Leave a Reply

Submit Comment



© 4871 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.