[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

c# - generate random number based on user input

Posted on 2012-09-18
19
Medium Priority
?
994 Views
Last Modified: 2012-09-20
Hello Experts,
Need to generate a random number based on user input.

For example:
User inputs 15
A 15 digit integer is returned

Thanks in advance!
0
Comment
Question by:j420exe1
  • 8
  • 6
  • 4
18 Comments
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 600 total points
ID: 38410617
Generate a 1 digit random number (0..9).  Add this to a string.  Loop and repeat the number of times the user has selected.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38410663
15 digits long would be to an int64 (Long in VB).  Do you really need a numeric variable?...or will a String do as Andy suggests?

How big can these "numbers" get?...and how do you plan on using them?
0
 

Author Comment

by:j420exe1
ID: 38410709
return value can be a string.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1400 total points
ID: 38410739
Andy's method will work just fine then.  Just make sure that the very first digit is not 0 (zero):
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = GetRandomNumber(15);
        }

        private Random R = new Random();

        private string GetRandomNumber(int length)
        {
            string number = "";
            for (int i = 1; i <= length; i++)
            {
                number = number + R.Next(i == 1 ? 1 : 0, 10).ToString();
            }
            return number;
        }

Open in new window

0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38410786
>>Just make sure that the very first digit is not 0 (zero):

I would disagree unless by eg. 3 digit it means a random number >=100 and <= 999.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38410806
True...just depends on how the number will be used.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38410830
Based on it being user input - in other words it could be 50 digit then it must be a string so for me 000000...000 is just as random as 100000....000

Anyway, wait and see.


ps.  If the first digit MUST be 1...9 then one doesn't need R.Next(i == 1 ? 1 : 0, 10).ToString()
Just force the first digit to be in that range then loop from 2 up to the max digit count.
0
 

Author Comment

by:j420exe1
ID: 38410849
first digit must be between 1 and 9
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38410869
If this is an academic excercise I would like to see what happens if the prof asks people for a 50 digit random number based on their code (I guess a few would only consider pure numeric possibilities and so have limited the range of values their code would work with).
What is the smiley for an evil grin ?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38410935
"What is the smiley for an evil grin ?"

Hmmm...

I like this one the best:

    }:-)
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38410942
Nice one.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38411156
No love for Andy?...    :-(
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38411498
@Idle_Mind
Thanks for the support.
I like to give hints and advice as to help one to help oneself (Better to learn to write code than just how to copy/paste) but it does have a negative effect at times.  I'm used to it by now (shit happens) - and I don't mind if it was a nice piece of code that was accepted.
0
 

Author Comment

by:j420exe1
ID: 38413546
Sorry Andy.  I tried to go back and give you points but couldn't after I already submitted.  I should have been more mindful when selecting a solution.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38413578
Up to you.  As I indicated earlier I can live with the current situation (and no grudges being held).

If you want to change things there is a link near the body of the question - request attention.
0
 

Author Comment

by:j420exe1
ID: 38413609
Submitted a request for attention.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38418146
Yay!  =)
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38418767
Thanks to both of you.  You are both gentlemen (or ladies)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

831 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