473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generating random strings

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
6 1787
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
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
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
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
Excellent, it works now...cheers!
*** Sent via Developersdex http://www.developersdex.com ***
May 31 '07 #6

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
2
by: Simon Wittber | last post by:
I'm building a web application using sqlalchemy in my db layer. Some of the tables require single integer primary keys which might be exposed in some parts of the web interface. If users can...
2
by: randomcz | last post by:
hi, i need help with generating random numbers; The task is to generate hundreds of random vectors, like 1,3,5,6,7 2,4,5,4,8 ... I used the current time as the random seed, but it turns...
14
by: avanti | last post by:
Hi, I need to generate random alphanumeric password strings for the users in my application using Javascript. Are there any links that will have pointers on the same? Thanks, Avanti
3
by: cameliachristina | last post by:
How do you generate random strings in C? Thank you.
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
14
by: Steven D'Aprano | last post by:
I have an application that will be producing many instances, using them for a while, then tossing them away, and I want each one to have a unique identifier that won't be re-used for the lifetime...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.