Solved

random name

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

896 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

13 Experts available now in Live!

Get 1:1 Help Now