Link to home
Start Free TrialLog in
Avatar of Jeff Fillegar
Jeff FillegarFlag for United States of America

asked on

c# - generate random number based on user input

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!
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Avatar of Jeff Fillegar

ASKER

return value can be a string.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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.
True...just depends on how the number will be used.
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.
first digit must be between 1 and 9
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 ?
"What is the smiley for an evil grin ?"

Hmmm...

I like this one the best:

    }:-)
Nice one.
No love for Andy?...    :-(
@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.
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.
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.
Submitted a request for attention.
Yay!  =)
Thanks to both of you.  You are both gentlemen (or ladies)