2

I wanted to generate random contact data (you know, first names, last names, city/state/zip and phone numbers) in .NET, and I found a few little snippets, but nothing really comprehensive.

I wrote these little methods to do this to save me (and maybe you) some time in the future. It’s obviously very easy to add more data to them depending on your needs. It generates things like this (just a few examples):

Michael Jones
461 Second Dr
Los Angeles, AK 12422
124-851-3783
James Smith
532 Sixth St
Columbus, LA 44714
446-576-5437
Richard Harris
261 Elm Pkwy
Las Vegas, OH 77006
769-302-7091
Patricia Thomas
68218 Maple Pkwy
Boston, NE 62280
944-388-4109
Elizabeth Martinez
332 Ninth Blvd
Sacramento, VT 79846
797-749-8472
Daniel Martin
65363 Elm Blvd
Tucson, CT 22140
221-474-1127
Robert Williams
403 First Dr
Milwaukee, MN 54432
543-200-2781
Joseph Taylor
133 Seventh St
Jacksonville, IA 39706
396-561-7144
David Miller
5542 Eighth Pkwy
Portland, NC 71998
719-286-8798
Linda White
844 Second Dr
Los Angeles, AZ 14291
142-911-1453

In C#... (see VB.NET down further)

        private Random GetRandomizer()
        {
            System.Threading.Thread.Sleep(1);
            return new Random(DateTime.Now.Millisecond);
        }

        private string GenerateName()
        {
            return GenerateFirstName() + " " + GenerateLastName();
        }

        private string GenerateFirstName()
        {
            string[] fNames = { "James", "John", "Robert", "Michael", "Mary", "William", "David", "Richard", "Charles", "Joseph",
                    "Thomas", "Patricia", "Christopher", "Linda", "Barbara", "Daniel", "Paul", "Mark", "Elizabeth", "Jennifer"};

            return GetRandomValueFromStringArray(fNames);
        }

        private string GenerateLastName()
        {
            string[] lNames = { "Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor",
                    "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia", "Martinez", "Robinson"};

            return GetRandomValueFromStringArray(lNames);
        }

        private string GenerateStreetNumber()
        {
            Random r = GetRandomizer();

            StringBuilder sB = new StringBuilder();

            for (int i = 0; i <= r.Next(2, 5); i++)
            {
                if (i == 0)
                {
                    // don't use a 0
                    sB.Append(r.Next(1, 9).ToString());
                }
                else
                {
                    sB.Append(r.Next(0, 9));
                }
            }

            return sB.ToString();
        }

        private string GeneratePhoneNumber()
        {
            Random r = GetRandomizer();
            int areaCode = r.Next(100, 999);

            if (areaCode.ToString().EndsWith("11"))
            {
                areaCode = areaCode + 1;
            }

            return areaCode.ToString() + "-" + r.Next(100, 999).ToString() + "-" + r.Next(1000, 9999).ToString();
        }

        private string GenerateStreetName()
        {
            string[] popularStreetNames = { "Second", "Third", "First", "Fourth", "Park", "Fifth", "Main", "Sixth", "Oak", "Seventh",
                            "Pine", "Maple", "Cedar", "Eighth", "Elm", "View", "Washington", "Ninth", "Wall" };
            string[] streetSuffixes = { "Dr", "St", "Pkwy", "Blvd" };

            return GetRandomValueFromStringArray(popularStreetNames) + " " + GetRandomValueFromStringArray(streetSuffixes);
        }

        private string GenerateAptOrSuite()
        {
            Random r = GetRandomizer();

            string[] prefixes = { "Apt", "Ste", "Bldg" };
            string[] suffixes = { "A", "B", "C", "D" };
            int numericalVal = r.Next(1, 300);

            return GetRandomValueFromStringArray(prefixes) + " " + numericalVal.ToString() + GetRandomValueFromStringArray(suffixes);
        }

        private string GenerateCity()
        {
            string[] cities = { "New York", "Los Angeles", "Chicago", "Houston", "Philadelphia", "Phoenix", "San Diego", "San Antonio", "Dallas", "Detroit",
                         "San Jose", "Indianapolis", "Jacksonville", "San Francisco", "Columbus", "Austin", "Memphis", "Baltimore", "Milwaukee", "Fort Worth",
                         "Charlotte", "El Paso", "Boston", "Seattle", "Washington", "Denver", "Portland", "Oklahoma", "Las Vegas", "Tucson",
                         "Long Beach", "Albuquerque", "New Orleans", "Cleveland", "Fresno", "Sacramento", "Mesa", "Atlanta" };

            return GetRandomValueFromStringArray(cities);
        }

        private string GenerateState(bool abbreviationOnly)
        {
            string[] states = { "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida",
                         "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine",
                         "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
                         "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
                         "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin",
                         "Wyoming" };
            string[] stateAbbr = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL",
                         "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KT", "LA", "MN",
                         "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH",
                         "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI",
                         "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA" };

            if (abbreviationOnly)
            {
                return GetRandomValueFromStringArray(stateAbbr);
            }
            else
            {
                return GetRandomValueFromStringArray(states);
            }
        }

        private string GenerateZipCode()
        {
            Random r = GetRandomizer();

            return r.Next(10000, 99999).ToString();
        }

        private string GetRandomValueFromStringArray(string[] sArray)
        {
            Random r = GetRandomizer();

            return sArray[r.Next(sArray.Length)];
        }

For an application in .NET Compact Framework, I need to load about 10MB worth of data stored in XML files into datasets. On a desktop, it takes approximately no time to load that data into a dataset, or write it back out to a XML file.

I thought to myself, "since this worked in less than 1 second on a PC, I should be able to do this on a mobile device in maybe 5-10 seconds" - acceptable for an application startup time. Right? Wrong. Try over 4 minutes. Nothing fancy, just a DataSet.ReadXml() and then iterating through and validating data.

Right now, you're thinking to yourself (because I'm writing this I get to choose), "why not use SQL CE or something?" - because the sole purpose of this data is to get enough data loaded into memory to get a head start on synchronizing it with the server, and SQL CE is not worth getting into as a dependency for this task. It's supposed to be as simple of a process as possible.

"Oh, that makes sense," you think. Thank you.

Knowing that 4 minutes for an applicaiton startup was a little bit excessive, I spent a few minutes turning it into a tab-delimited file instead.

The results? All the data loaded in 20 seconds. It's not fancy, it's not flexible, but it works. And that's what I care about at the end of the day.