Solved

# c# - generate random number based on user input

Posted on 2012-09-18
924 Views
Hello Experts,
Need to generate a random number based on user input.

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

0
Question by:j420exe1

LVL 43

Assisted Solution

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 85

Expert Comment

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

return value can be a string.
0

LVL 85

Accepted Solution

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;
}
``````
0

LVL 43

Expert Comment

>>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 85

Expert Comment

True...just depends on how the number will be used.
0

LVL 43

Expert Comment

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

first digit must be between 1 and 9
0

LVL 43

Expert Comment

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 85

Expert Comment

"What is the smiley for an evil grin ?"

Hmmm...

I like this one the best:

}:-)
0

LVL 43

Expert Comment

Nice one.
0

LVL 85

Expert Comment

No love for Andy?...    :-(
0

LVL 43

Expert Comment

@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

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 43

Expert Comment

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

Submitted a request for attention.
0

LVL 85

Expert Comment

Yay!  =)
0

LVL 43

Expert Comment

Thanks to both of you.  You are both gentlemen (or ladies)
0

## Featured Post

### 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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…