Solved

random name

Posted on 2011-09-21
3
214 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now