Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Generating the random numbers using C#

Posted on 2006-11-22
Medium Priority
194 Views
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
Question by:run2004
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 23

Expert Comment

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

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

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

Expert1701 earned 600 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);

}

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

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

## Featured Post

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
###### Suggested Courses
Course of the Month11 days, 10 hours left to enroll