Generating random strings

Discussion in 'ASP .Net' started by Mike P, May 31, 2007.

  1. Mike P

    Mike P 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 Developersdex http://www.developersdex.com ***
     
    Mike P, May 31, 2007
    #1
    1. Advertising

  2. On May 31, 10:31 am, Mike P <> wrote:
    > 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
     
    Alexey Smirnov, May 31, 2007
    #2
    1. Advertising

  3. Mike P

    Mike P 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 Developersdex http://www.developersdex.com ***
     
    Mike P, May 31, 2007
    #3
  4. On May 31, 11:04 am, Mike P <> 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 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.
     
    Alexey Smirnov, May 31, 2007
    #4
  5. On May 31, 10:31 am, Mike P <> wrote:
    > 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.
     
    Alexey Smirnov, May 31, 2007
    #5
  6. Mike P

    Mike P Guest

    Mike P, May 31, 2007
    #6
  7. 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 <>
    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 ***
     
    Samuel R. Neff, May 31, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  2. Replies:
    7
    Views:
    524
    Jim Langston
    Jul 13, 2005
  3. globalrev
    Replies:
    4
    Views:
    810
    Gabriel Genellina
    Apr 20, 2008
  4. avanti
    Replies:
    14
    Views:
    230
    Chad Burggraf
    Jan 4, 2007
  5. VK
    Replies:
    15
    Views:
    1,315
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page