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

x
Solved

# random numbers

Posted on 2006-06-24
Medium Priority
354 Views
I am trying to create random 22 digit numbers...

I tried the randomclass.Next with 22 9's but I get an error that int cannot be a long
Example code would be great.....

any ideas????

0
Question by:bman9111
[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
• 3

LVL 86

Expert Comment

ID: 16975540

private void button1_Click(object sender, EventArgs e)
{
label1.Text = RandomNumber(22);
}

private string RandomNumber(byte length)
{
StringBuilder sb = new StringBuilder();
Random R = new Random();
for (int i = 1; i <= length; i++)
{
if (i == 1)
{
sb.Append(R.Next(1, 10).ToString());
}
else
{
sb.Append(R.Next(0, 10).ToString());
}
}
return sb.ToString();
}

or?

private void button1_Click(object sender, EventArgs e)
{
label1.Text = RandomNumber(22);
}

private string RandomNumber(byte length)
{
StringBuilder sb = new StringBuilder();
do
{
sb.Append(Guid.NewGuid().ToString("N").ToLower()
.Replace("a","").Replace("b","").Replace("c","")
.Replace("d","").Replace("e","").Replace("f",""));
if (sb[0] == '0')
{
sb.Remove(0, 1);
}
} while (sb.Length < length);
sb.Length = length;
return sb.ToString();
}
0

LVL 64

Expert Comment

ID: 16975574
Hi bman9111;

Here is another way to do what you want.

Dim rand As New Random(Now.Second())
Dim rn As Decimal = rand.Next(1000000000, 2147483647)
rn = (rn * 10000000000 + rand.Next(1000000000, 2147483647)) * 100 + rand.Next(100)
Label1.Text = rn.ToString()

Fernando
0

LVL 64

Accepted Solution

Fernando Soto earned 500 total points
ID: 16975605
And in C# it would be

Random rand = new Random(DateTime.Now.Second);
decimal rn = rand.Next(1000000000, 2147483647);
rn = (rn * 10000000000 + rand.Next(1000000000, 2147483647)) * 100 + rand.Next(100);
label1.Text = rn.ToString();

Fernando
0

LVL 8

Author Comment

ID: 16976056
what does the number 2147483647

represent..... can that be anything????
0

LVL 64

Expert Comment

ID: 16976417
Because the Random object only returns a positive integer number and integer range is between -2,147,483,648 to 2,147,483,647, 2147483647 is the largest integer value possible.

To your question, "can that be anything????"

In the code sample I gave it can be anything greater then 1000000000 but less then or equal to 2147483647. The reason is that you want a 22 digit number, so concatenating two 10 digit number and a two digit number will give a 22 digit number.
0

## Featured Post

Question has a verified solution.

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

C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
###### Suggested Courses
Course of the Month11 days, 7 hours left to enroll