Generating random strings

M

Mike P

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);
}
}
 
G

Guest

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
 
M

Mike P

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;
}
}
 
G

Guest

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.
 
G

Guest

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.
 
S

Samuel R. Neff

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top