How do I create random numbers and alphanum massively in C#

Hi guys,

How do i create random dummie records to be written into a file testdb.txt
in this format with delimited |


decimal(10)|smallint(4)|smallint(4)|char(20)|decimal(10)

For example (unsorted):

20330|2  |140|HRR8353904    |1600000104661
3892|39|21|GRG893FEFE|339209230

Basically the decimal(10) can be any number from 1-10 digits and the char(20) can be any alphanumeric in caps, without special characters, up till 20 chars.

Thanks!
jedistarAsked:
Who is Participating?
 
anarki_jimbelConnect With a Mentor Commented:
What do you mean "sample"? Sample code? If yes see below. Or pls clarify.

     public class RandomStringGenerator
    {
        private static string RandomString(int size, Random random)
        {
            StringBuilder builder = new StringBuilder();

            char ch;
            while(builder.Length<12){
                int charCode = Convert.ToInt32(Math.Floor(74 * random.NextDouble() + 48));
                if ((charCode >= 48 && charCode <= 57) || (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 121))
                {
                    ch = Convert.ToChar(charCode);
                    builder.Append(ch);
                }
            }
            return builder.ToString();
        }


        public static void Main(string[] args)
        {
            Random random = new Random();

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(RandomString(12, random));
            }

            Console.ReadLine();
        }
    }


============== OUTPUT (example) ====================

CNauZQHgoxRQ
dRNaSFwnn5KN
0LxlC65xU7U1
cUZB3ijmIjjF
2NYf3Ggh2dK4
RCGQamIUvHY2
Sj2YuJFxFLtR
ppxGZ1HC6iqO
790A2sCvoSJP
Aq5IGyMmEeWZ
0
 
anarki_jimbelCommented:
I believe you still need to generate each number separately and later combine them to a string.
To generate random string follow http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx. They have quite good approach using conversion from ascii codes to characters. If you will need both upper and lower case character mixed you'll need to modify procedure (bit tricky)
0
 
anarki_jimbelCommented:
really it's quite easy to modify the procedure (see the link above) to generate strings with lower in upper case letters. There will be some overhead however. You generate random number and add 65 - this is ascii code. You may generate number not by multiplying by 26 but 26+26+5=57. 5 is for five characters between lower and upper case letters in ascii table ([,] etc). So if ascee code is 091 to 096 (decimal) you just exclude the case and try again. The algorithm will work nearly 10% slower.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
anarki_jimbelCommented:
...lower and upper ..." sorry for typos.
0
 
jedistarAuthor Commented:
Do you have a sample to generate etc..

FJ2KELD93904
0
 
jedistarAuthor Commented:
wheres size used
0
 
anarki_jimbelCommented:
Is that a question?

In my code the function randomString should be public, of course.
It has two parameters: (int size, Random random)

First parameter is the size of the outout string, 12 in my example.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.