472,325 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 software developers and data experts.

generate randaom password(1 uuper case letter, 1 digit and 6 character length)


I want generate random password. it should contain at least 1 upper case letter, 1 digit, 6 character length.

I can use Membership.GeneratePassword(-, -), but its not having numbers some times.

Any help please?

Thank you.
Jul 16 '10 #1
1 5691
Hi Ramanak,

Here's a quick algorithm to do want you want to do. Sorry it's not the cleanest but it works. Didn't have plenty of time to test it though.

You can change the minimum and maximum number of chars. You can also add other chars (symbols, etc) if you want. You can also make it cleaner, but don't create the Random object in the function or it will always generate the same password.

Again, sorry it's not the cleanest but I think you get the idea.

Have a good day.


Expand|Select|Wrap|Line Numbers
  1.         protected String RandomPassword()
  2.         {
  3.             String pwd = "";
  4.             int length = 0;
  5.             int index = 0;
  6.             int numericIndex = -1;
  7.             int upperCaseIndex = -1;
  9.             //Length of your password
  10.             length = rnd.Next(MINLENGTH, MAXLENGTH);
  12.             // You generate a password of the desired length
  13.             for (int i = 0; i < length; i++)
  14.             {
  15.                 // Generate an index that smaller than the size of your allowed chars
  16.                 index = rnd.Next(0, allowedChars.Length);
  18.                 pwd += allowedChars[index];
  19.             }
  21.             ////*********************************************************
  22.             // We make sure that there is at least one numeric
  23.             // Replace one random char by a numeric
  24.             numericIndex = rnd.Next(0, pwd.Length);
  26.             // Generate a numeric, delete one char and replace it with a numeric
  27.             index = rnd.Next(0, numericChars.Length);
  28.             pwd = pwd.Remove(numericIndex, 1);
  29.             pwd = pwd.Insert(numericIndex, numericChars[index].ToString());
  30.             ////*********************************************************
  33.             ////*********************************************************
  34.             // We make sure that there is at least one uppercase
  35.             // Replace one random char by a numeric
  36.             upperCaseIndex = rnd.Next(0, pwd.Length);
  38.             // We make sure our uppercase index is different
  39.             // from our numeric index or we will overwrite our
  40.             // only numeric value with our uppercase value
  41.             while (upperCaseIndex == numericIndex)
  42.             {
  43.                 upperCaseIndex = rnd.Next(0, pwd.Length);
  44.             }
  46.             // Generate a numeric, delete one char and replace it with a numeric
  47.             index = rnd.Next(0, upperCaseChars.Length);
  48.             pwd = pwd.Remove(upperCaseIndex, 1);
  49.             pwd = pwd.Insert(upperCaseIndex, upperCaseChars[index].ToString());
  50.             ////*********************************************************
  52.             return pwd;
  53.         }
  55.         Random rnd = new Random();
  56.         const int MINLENGTH = 6;
  57.         const int MAXLENGTH = 20;
  58.         const String allowedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  59.         const String upperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  60.         const String numericChars = "0123456789";
Jul 16 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Adams-Blake Co. | last post by:
I want to allow the user to enter her own username and password. I want to validate the password the way lots of programs do.... that it has to...
by: avillela | last post by:
I'm looking for a password generator. example: I pass as parameter a string and the function return a password.(string) can anyone provide me...
by: learn.2005 | last post by:
Hello Friends! I am Manu Gupta, an IT Engineering student, learning PHP presently. I want to match user name and pwd from MySQL, it shows case...
by: bigd7788 | last post by:
Ladies and gents, Need a little help with the following: Write a program that will accept a string from the user and evaluate it as a password,...
by: Chris | last post by:
I want to ensure a password has a least one lower case letter, one upper letter and a number. I'm a bit of a newbie but I understand you can use the...
by: Army1987 | last post by:
Is there anything wrong with this program? It seems to behave strangely if I give stdin EOF when asked for the character set... /* BEGIN pwdgen.c...
by: DarthPeePee | last post by:
Hello everyone. I am working on a Password Strength Meter and I am running into 1 problem that I would like to fix. When pressing the "Clear...
by: ashurack | last post by:
I found a stored procedure online a while back and want to inplement it. The only problem is that it doesn't check to see if the number generated is...
by: ccarter45 | last post by:
I'm new to java and writing a program that accepts user input for a password and it has to meet the following requirements: 1. At least 6...
by: Jonathan Boivin | last post by:
If I could give an advice on your method, in fact, what I would do.. is : Generate a password with your lower security set of characters. Then upper...
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.