Solved

Generating the random numbers using C#

Posted on 2006-11-22
5
179 Views
Last Modified: 2010-05-18
Hi,

Can you please provide me the working code in C# to generate the random numbers for the following requirements:

1) The Random numbers will have only Alphanumeric values (1,2,3,A,B,c...etc).
2) The function should accept the precision. i.e how much will be the length of the random numbers.
3) The function should accept the input for the count. For example if I input 3 with precision 8. The the code should generate three random numbers, each of length 8.
4) The output should be in the format: Number 1: ABCDEFGH, Number 2:ZZZZZZZ and so on

How can I do this?

Thanks for the help.

Run2004
0
Comment
Question by:run2004
  • 2
  • 2
5 Comments
 
LVL 23

Expert Comment

by:adathelad
ID: 17996888
Hi run2004,

This function will return an array of random numbers as you require:
private string[] GenerateRandomNumbers(int Count, int Precision)
        {
            string[] numbers = new String[Count];
            System.Text.StringBuilder numberBuilder = new System.Text.StringBuilder();
            string newNumber;

            for (int loop=0; loop < Count; loop++)
            {
                numberBuilder.Length = 0;
                while (numberBuilder.ToString().Length < Precision)
                {
                    numberBuilder.Append(System.Guid.NewGuid().ToString().Replace("-", ""));
                }

                newNumber = numberBuilder.ToString();
                if (newNumber.Length > Precision)
                {
                    newNumber = newNumber.Substring(0, Precision);
                }

                numbers[loop] = newNumber;
            }

            return numbers;
        }

0
 
LVL 23

Accepted Solution

by:
adathelad earned 350 total points
ID: 17996929
To get the output in the format "Number 1: .....", you can use this version:
private string GenerateRandomNumbers(int Count, int Precision)
        {
            System.Text.StringBuilder numberBuilder = new System.Text.StringBuilder();
            System.Text.StringBuilder numberString = new System.Text.StringBuilder();
            string newNumber;

            for (int loop=0; loop < Count; loop++)
            {
                numberBuilder.Length = 0;
                while (numberBuilder.ToString().Length < Precision)
                {
                    numberBuilder.Append(System.Guid.NewGuid().ToString().Replace("-", ""));
                }

                newNumber = numberBuilder.ToString();
                if (newNumber.Length > Precision)
                {
                    newNumber = newNumber.Substring(0, Precision);
                }

                numberString.Append("Number ");
                numberString.Append((loop + 1).ToString());
                numberString.Append(": ");
                numberString.Append(newNumber);

                if ((loop+1) < Count) { numberString.Append(", "); }
            }

            return numberString.ToString();
        }
0
 
LVL 1

Author Comment

by:run2004
ID: 17999589
This is a good one, but I just wanted to know how good it is to use the random class to generate the random numbers. There is one more observation, the output doesn't contain upper case letters. Is there a way to have both in one place.

Thanks for the fast response.
0
 
LVL 11

Assisted Solution

by:Expert1701
Expert1701 earned 150 total points
ID: 18001422
Here is a sample of output,

  T9bhygXK
  by1Gl7aW
  bnhMe6iZ

produced by this program:

  static Random random = new Random();

  static void Main()
  {
    foreach (string result in GenerateRandoms(3, 8))
      Console.WriteLine(result);

    Console.ReadLine();
  }

  static string[] GenerateRandoms(int number, int precision)
  {
    string[] list = new string[number];
   
    for (int i = 0; i < number; i++)
      list[i] = GenerateRandom(precision);
   
    return list;
  }

  static string GenerateRandom(int precision)
  {
    char[] list = new char[precision];

    for (int i = 0; i < precision; i++)
    {
      int next = random.Next() % 62;
      if (next < 10)
        list[i] = (char)('0' + next);
      else if (next < 36)
        list[i] = (char)('A' + next - 10);
      else
        list[i] = (char)('a' + next - 36);
    }

    return new string(list);
  }
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 18017562
run2004, did the sample application I posted meet your requirements?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now