Solved

random numbers

Posted on 2006-06-24
5
333 Views
Last Modified: 2010-04-16
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
Comment
Question by:bman9111
  • 3
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16975540
How about?

        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 62

Expert Comment

by:Fernando Soto
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 62

Accepted Solution

by:
Fernando Soto earned 125 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

by:bman9111
ID: 16976056
what does the number 2147483647

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

Expert Comment

by:Fernando Soto
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connecting database in Xamarin? 6 52
How to get the web content with different encodings 2 28
Syntax Error 2 47
Entity Framework 7 32
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

896 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

14 Experts available now in Live!

Get 1:1 Help Now