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
Solved

random name

Posted on 2011-09-21
3
223 Views
Last Modified: 2012-05-12
I am creating a random passwork.
I loop a list and want to create a new password for each. But the same password comes up. Do you know why that can is?

 foreach (User s in sList)
{
      if (s.Password1 == "")
      {
              s.Password1 = CreateRandomPassword();
       }
}

private static string CreateRandomPassword()
    {
        Random rd1 = new Random();
        Random rd2 = new Random();
        string password = "";
        string[] names = new string[] { "Car", "House", "Building" };
        for (int i = 0; i < 5; i++)
        {
            password = names[rd1.Next(names.Length - 1)] + rd2.Next(1, 99);
        }
        return password;
    }
0
Comment
Question by:johnkainn
3 Comments
 
LVL 3

Expert Comment

by:paradox_cla
ID: 36573098
The problem is call "new Random()".
The call should not be repeated.
Call "new random" use the PC timer.
0
 
LVL 2

Accepted Solution

by:
arundhaj earned 250 total points
ID: 36573130
If you wanted to generate random password, one possible approach is
public static String CreateRandomPassword()
{
    String possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    StringBuilder password = new StringBuilder();
    Random rand = new Random();
    int lengthOfPassword = 5;

    for (int i = 0; i < lengthOfPassword; i++)
        password.Append(possible.Substring(rand.Next(possible.Length), 1));

    return password.ToString();
}

Open in new window

0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 36574408
I agree with Paradox...MOVE your Random variable to outside your function at Class level like this:
*Note that you only need ONE instance*
private Random Rnd = new Random();

    private static string CreateRandomPassword()
    {
        string password = "";
        string[] names = new string[] { "Car", "House", "Building" };
        for (int i = 0; i < 5; i++)
        {
            password = names[Rnd.Next(names.Length - 1)] + Rnd.Next(1, 99).ToString();
        }
        return password;
    }

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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