Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hello, I want to do this work by threading. help me how...

private static string MakePassword4(int pl)
       {
           string possibles =
           "0123456789";
           char[] passwords = new char[pl];
           Random rd = new Random();
           {
               for (int i = 0; i <1000; pl; i++)
               {
                   passwords[i] = possibles[rd.Next(0, possibles.Length)];
               }
           }
           return new string(passwords);
       }


private void button1_Click(object sender, EventArgs e)
 {
 

         listView1.View = View.Details;
         listView1.Items.Clear();
         for (int j = 0; j<1000;j++)         {
             temp = MakePassword11(length);
             Refresh();
             listView1.GridLines = true;
             listView1.Items.Add(temp.ToString());
         }
        return;
     }

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 22-Mar-11 2:50am
v3
Comments
Ankit Rajput 22-Mar-11 7:38am    
Hi Satendra,

Can you please explain in detail which functionality you want to put in threading?

Regards
Ankit
[no name] 22-Mar-11 7:51am    
Also, the posted code is quite unclear and should not work at all. In button1_Click(): for (int j = 0; j <str;> This loop looks a little mishappen. Also, no variable 'str' is declared. Then, a method MakePasswordll() is called, the posted code only has a MakePassword4() method. Is this the one to be called?

Why would you want to do that via threading? Do you think it is going to take considerable amounts of time?
If your MakePassword11 method is anything like your MakePassword4 method, then there will be more overhead in setting up and destroying the new thread than should be taken up by the method - and it will be nowhere near as clear what is happening.

Can I also suggest that a password which consists only of numeric digits is a bit on the insecure side? I.e. a brute force approach will crack it pretty quickly...

What are you trying to achieve?

BTW: Change your UserName: posting your email address to public forums is an invitation to spam...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Mar-11 13:12pm    
Agree with you, my 5.
--SA
#realJSOP 22-Mar-11 14:33pm    
My solution is a better way to do it. Someone voted it a 1 with no explanation.
#realJSOP 22-Mar-11 15:06pm    
Why he's creating 1000 passwords is beyond me, but I think he's just generating a temporary password and the user will have to change it the first time he logs on.
OriginalGriff 22-Mar-11 15:41pm    
I think he is giving the user a choice! :laugh:
Why else put 1,000 random items in a ListView?
Maybe that's the test: one of these is your password: spot it and I'll let you log in...
#realJSOP 22-Mar-11 18:29pm    
That would certainly be a novel approach. "Here's a crapload of possible passwords, pick yours from the unsorted list."
Instead of going through all that, why don't you just create a GUID and give them a substring of the generated GUID?

C#
string password = Guid.NewGuid().ToString("N").Substring(5, 8);


No threading is needed.

EDIT ==============

The 1 vote on this answer is unjustified. My solution is a) Faster than the what was posted in the original question, and b) generates STRONGER random passwords because they may include alphabetic characters as well as numbers.

If you want proof of the randomness of using the Guid.NewGuid method, run this code in a WinForms app:

C#
private void CreateGuids()
{
    HashSet<string> hash = new HashSet<string>();
    for (int i = 0; i < 1000; i++)
    {
        hash.Add(Guid.NewGuid().ToString("N").Substring(5,8));
    }
    MessageBox.Show(string.Format("{0} unique passwords generated.", hash.Count));
}


There is no denying that this is a better solution than building a password one character at a time, and calling Random.Next for each character.
 
Share this answer
 
v5
Comments
Christoph Keller 22-Mar-11 10:46am    
SubString'ing a Guid would produce a lot of equal passwords, so this would not be a good solution (expecially if he want to generate 1000 PW's without any delay).

Just my 5 cents ;)
#realJSOP 22-Mar-11 14:12pm    
You obviously have never played with the NewGuid() method. They're completely random. Run this code in a WinForms app and see what you get:
private void CreateGuids()
{
HashSet<string> hash = new HashSet<string>();
for (int i = 0; i < 1000; i++)
{
hash.Add(Guid.NewGuid().ToString("N").Substring(5,8));
}
MessageBox.Show(string.Format("{0} unique passwords generated.", hash.Count));
}

Just in case you're not familiar with the HashSet collection, it doesn't allow duplicate keys to be added. I ran this code several times, and ALWAYS got 1000 unique passwords.
Christoph Keller 22-Mar-11 16:43pm    
I never said that a GUID is not unique, a GUID self is random for sure.

But what I mean is that if you Substring a Guid and shorten it, the possibility of duplicates is much higher.

O.k., I did not test your source (now I did and saw that no duplicates are generated), nor I did not see that you put it into a HashSet. Sorry for that. :)

I just saw the "Substring" and started to write ;)
So, sorry again!
#realJSOP 22-Mar-11 18:27pm    
The use of the hashset was merely to prove that no duplicates were found. If the guy wanted to do it right, he'd create a password until he's got 1000 unique ones instead of merely trying 1000 times for 1000 unique ones.
OriginalGriff 22-Mar-11 15:39pm    
JSOP is correct: Guids are completely random, and the phase space of a Guid is 2 to the power of 128. This equates to 3.4 times 10 to the power of 38. A three, followed by 38 zeros. That means that there is a 1 in 3*10^38 chance of two duplicates. Taking substrings of this does not reduce the randomness, it just reduces the phase space. The randomness of the Guid is likely to be higher than the randomness of the OP's original method, because the random number generator is not re-started each time - which is clock based, and in a sufficiently fast computer (or multithreaded environment with sufficient cores) can be persuaded to generate the same sequence.

Guid generation is quick as well: even on this not-very-quick-at-all PC, 1,000,000 guids takes less that 1/2 a second...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900