?
Solved

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

Posted on 2007-07-22
7
Medium Priority
?
543 Views
Last Modified: 2012-06-27
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!
0
Comment
Question by:jedistar
  • 5
  • 2
7 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 19545122
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
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 19545152
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
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 19545155
...lower and upper ..." sorry for typos.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jedistar
ID: 19552884
Do you have a sample to generate etc..

FJ2KELD93904
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 19553258
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
 

Author Comment

by:jedistar
ID: 19553727
wheres size used
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 19560227
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Six Sigma Control Plans
Loops Section Overview

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question