Solved

random name

Posted on 2011-09-21
3
226 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
[X]
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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sort GridView by ID Descending 1 24
linq, c# 8 33
"The resource cannot be found" 1 24
How to set focus on a dynamic control 18 21
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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