random password

Posted on 2008-10-17
Last Modified: 2012-05-05
i am using this code to generate a random number:
Session["Id"] = new Random().Next(1, 1000);
how can i also include uppercase and or lowercase characters to make it like a random pasword. but it must be unique.

or is there another way to generate a random but unique password and indicate how long it can be in and classic asp?

Question by:eaweb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +5

Expert Comment

ID: 22743864

Hi eaweb

Can't post the code here (i should think it), but you can use:

chr(Random().Next(65,90)) to obtain uppercase random letters
chr(Random().Next(97,122)) to obtain lowercase random letters.
Also, if you use range 65 to 122 you can include some symbols ([\]^_`)
By lowering 65 to 58, you may obtain too: :;<=>?@


Expert Comment

ID: 22743886

Oopss.. it leave me before I've finished.

You should stablish which it's going to be the length of the random password, and iterate through this functions.
You also may do a Select Case from a random 1 to 3, and according to that random, call one of the three options (numbers, lower or uppercase)

Sorry for only giving you the ideas, but I do not have reachable my working framework.
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Expert Comment

ID: 22744130
What itkamaraj said -- generating a GUID (globally unique ID) is a pretty good way to go.
LVL 55

Accepted Solution

Jaime Olivares earned 500 total points
ID: 22744158
you can build your own function:

public string CreateRandomPassword(int size)
      Random rand = new Random();

      string pass = "";
      for (int i=0; i<size;)
             char val = (char)rand.Next(48,127);
             if (char.IsLetterOrDigit(val))
                  pass += val; i++;
      return pass;

So you can use as:

Session["Id"] = CreateRandomPassword(10);
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22744423
The author stated:

    "...a random but unique password..."

As the posts have shown thus far, there are MANY ways to generate a random string of characters that can be used as a password.

What has not been addressed is the "UNIQUE" aspect in the requirements.

Unique with respect to what?  Do you have a set of all previously generated passwords that you need to check against to prevent duplicates?

Give us more details...

Author Comment

ID: 22744806
oh sorry,
i need the generation to be based on globally unique ID.

i need a way to do it in vb and classic asp vb
LVL 13

Expert Comment

ID: 22745006
use Guid.NewGuid()
this will give a globally unique id every time
this class is a part of dotnet framework

Author Comment

ID: 22745052
how can i convert the following .net code into classic asp code?

Public Function GetRandomPasswordUsingGUID(ByVal length as Integer) as String  
  'Get the GUID
  Dim guidResult as String = System.Guid.NewGuid().ToString()
  'Remove the hyphens
  guidResult = guidResult.Replace("-", String.Empty)
  'Make sure length is valid
  If length <= 0 OrElse length > guidResult.Length Then
    Throw New ArgumentException("Length must be between 1 and " & guidResult.Length)
  End If
  'Return the first length bytes
  Return guidResult.Substring(0, length)
End Function
LVL 45

Expert Comment

ID: 22746249
In order to ensure uniqueness, you will have to use all 32 characters of the GUID.  If your password is less than the GUID length, you will need to combine the GUID bytes in a way that ensures uniqueness.

Otherwise, you will need to retain/persist the passwords you've generated and do a look-up to ensure uniqueness before issuing a new password.
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22747075
Right...there isn't any guarantee at all that GUIDs are actually unique.  They don't have any notion of what has been generated before...

The "uniqueness" is simply based on the fact that they are so long that there are literally millions of combinations that can be produced and it is unlikely to generate the same sequence in any short period.

Directly from the documentation:

    "There is a very low probability that the value of the new Guid is all zeroes or equal to any other Guid."

By shortening the length of the value you are decreasing the number of combinations to pick from and increasing the likelihood of a repeat.  The actual probability depends on how often you generate values, how long you need a generated value to remain "unique", and whether you are disposing of the values afterwards or need to persist them so you know they have been used before.
LVL 45

Expert Comment

ID: 22747681

1. What is the big picture for this question?  Why would passwords need to be guaranteed/absolutely unique versus probably unique?

2. How long are these passwords?

3. What kind of character set will comprise these passwords?

4. Are these passwords for use by humans or machines?  (memorability might be important)
LVL 45

Expert Comment

ID: 23000873
PAQ with no refund

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question