By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

Generating random strings

P: n/a
I am generating 12 random strings and my code works fine when I step
through, but when I then let it run without stepping through and
populate a listbox with the array I am building, I get the same value
repeated 12 times. Here is my code :

protected void btnGenerate_Click(object sender, EventArgs e)
{
string strDomainName = txtDomainName.Text.ToString();
string strEmailAddress = "";
string strEmailAddressComplete = "";

ArrayList arrEmailAddresses = new ArrayList();

for (int i = 0; i <= 12; i++)
{
//create 12 random email addresses for domain
EmailAddressGenerator emg = new EmailAddressGenerator();
strEmailAddress = emg.GenerateEmailAddress();
strEmailAddressComplete = strEmailAddress + "@" +
strDomainName;

//add email addresses to array
arrEmailAddresses.Add(strEmailAddressComplete);
}

foreach (string address in arrEmailAddresses)
{
lstEmailAddresses.Items.Add(address);
}
}

*** Sent via Developersdex http://www.developersdex.com ***
May 31 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On May 31, 10:31 am, Mike P <mike.p...@gmail.comwrote:
I am generating 12 random strings and my code works fine when I step
through, but when I then let it run without stepping through and
populate a listbox with the array I am building, I get the same value
repeated 12 times. Here is my code :

protected void btnGenerate_Click(object sender, EventArgs e)
{
string strDomainName = txtDomainName.Text.ToString();
string strEmailAddress = "";
string strEmailAddressComplete = "";

ArrayList arrEmailAddresses = new ArrayList();

for (int i = 0; i <= 12; i++)
{
//create 12 random email addresses for domain
EmailAddressGenerator emg = new EmailAddressGenerator();
strEmailAddress = emg.GenerateEmailAddress();
strEmailAddressComplete = strEmailAddress + "@" +
strDomainName;

//add email addresses to array
arrEmailAddresses.Add(strEmailAddressComplete);
}

foreach (string address in arrEmailAddresses)
{
lstEmailAddresses.Items.Add(address);
}
}

*** Sent via Developersdexhttp://www.developersdex.com***
It seems that GenerateEmailAddress is not working properly.

Try for example this

public string GenerateEmailAddress()
{
return System.Guid.NewGuid().ToString();
}

to see if I'm right, or not

May 31 '07 #2

P: n/a
I've just checked and you're right Alexey. Here is my
EmailAddressGenerator class :

public class EmailAddressGenerator
{
protected Random rGen;
protected string[] strCharacters = {
"1","2","3","4","5","6","7","8","9","0","a","b","c ","d","e","f","g","h",
"i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z "};

public EmailAddressGenerator()
{
rGen = new Random();
}

public string GenerateEmailAddress()
{
int p = 0;
int i = 11;
string strEmailAddress = "";

for (int x = 0; x<= i; x++)
{
p = rGen.Next(0,36);
strEmailAddress += strCharacters[p];
}

return strEmailAddress;
}
}

*** Sent via Developersdex http://www.developersdex.com ***
May 31 '07 #3

P: n/a
On May 31, 11:04 am, Mike P <mike.p...@gmail.comwrote:
I've just checked and you're right Alexey. Here is my
EmailAddressGenerator class :

public class EmailAddressGenerator
{
protected Random rGen;
protected string[] strCharacters = {
"1","2","3","4","5","6","7","8","9","0","a","b","c ","d","e","f","g","h",
"i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z "};

public EmailAddressGenerator()
{
rGen = new Random();
}

public string GenerateEmailAddress()
{
int p = 0;
int i = 11;
string strEmailAddress = "";

for (int x = 0; x<= i; x++)
{
p = rGen.Next(0,36);
strEmailAddress += strCharacters[p];
}

return strEmailAddress;
}

}

*** Sent via Developersdexhttp://www.developersdex.com***
Well, actually, GenerateEmailAddress is correct :-) When you create a
new instance of EmailAddressGenerator, the same seed is used in new
Random, and the same series of numbers is generated.

Move new EmailAddressGenerator out of cycle

For example:

EmailAddressGenerator emg = new EmailAddressGenerator();
for (int i = 0; i <= 12; i++)

and it should help.

May 31 '07 #4

P: n/a
On May 31, 10:31 am, Mike P <mike.p...@gmail.comwrote:
I am generating 12 random strings and my code works fine when I step
through, but when I then let it run without stepping through and
Random() is a time dependent object. When you step through you touch

EmailAddressGenerator emg = new EmailAddressGenerator();

each time in a different time. When you run the code without debugger
it executes quickly and you see no difference in generated numbers.

May 31 '07 #5

P: n/a
Excellent, it works now...cheers!
*** Sent via Developersdex http://www.developersdex.com ***
May 31 '07 #6

P: n/a

move rGen to a static field with a static initializer so it's only
created once and shared. Also lock access to the field since Random
is not thread safe.

Moving rGen to a static will fix the issue without imposing
restrictions on how EmailAddressGenerator is used.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 31 May 2007 02:04:03 -0700, Mike P <mi*******@gmail.com>
wrote:
>I've just checked and you're right Alexey. Here is my
EmailAddressGenerator class :

public class EmailAddressGenerator
{
protected Random rGen;
protected string[] strCharacters = {
"1","2","3","4","5","6","7","8","9","0","a","b"," c","d","e","f","g","h",
"i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z "};

public EmailAddressGenerator()
{
rGen = new Random();
}

public string GenerateEmailAddress()
{
int p = 0;
int i = 11;
string strEmailAddress = "";

for (int x = 0; x<= i; x++)
{
p = rGen.Next(0,36);
strEmailAddress += strCharacters[p];
}

return strEmailAddress;
}
}

*** Sent via Developersdex http://www.developersdex.com ***
May 31 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.