C# - Constantly adding 9 digits -
as title says, i'm developing in c#.
i'd generate number has more 9 numbers, thought generating numbers, follow 9.
instance, if want generate number has 10 digits - i'd generate first number has 9 numbers, , number has 1 digit.
if generate number 20, i'd generate first 2 numbers 9 digits each, , number 2 digits.
i've tried:
for (int = 0, j = (int.parse(inputrandom.text) % 9 == 0 ? int.parse(inputrandom.text) % 9 : int.parse(inputrandom.text) % 9 + 1); < j; i++) if (i < (int.parse(inputrandom.text) % 9 == 0 ? j % 9 : j % 9 + 1) - 1) numtosend += (biginteger)r.next(int.parse(1 + zero((int.parse(inputrandom.text) % 9 + 8) * (inputrandom.text.length - 1)))); else numtosend += (biginteger)r.next(int.parse(1 + zero(int.parse(inputrandom.text) % 9 * 9)));
the method 0 returns string 0, times number specified in. i.e zero(1)
return: "0"
need method because method next(int32)
can return number, specified between brackets. i.e next(10)
return number between 0 , 9.
my goal
i want generate number number of digit above 9, , put number string numtosend
. cannot this, because method next()
can generate int
s.
even after edit, don't find question clear. however, think understand trying generate random biginteger
value, , want random value within range defined specific number of digits.
for example, if ask 10-digit value, value between 0 , 9999999999 should returned. if ask 20-digit value, value between 0 , 99999999999999999999 should returned.
based on understanding, believe method should accomplish asking:
biginteger nextrandom(random random, int digitcount) { biginteger result = 0; while (digitcount-- > 0) { result = result * 10 + random.next(10); } return result; }
note number returned have fewer digitcount
actual digits in it. in fact, there's 1-in-10 chance of happening. , 1-in-100 chance of result having fewer (digitcount
- 1) digits, etc.
if want different that, please more specific exact requirements of output want (read https://stackoverflow.com/help/how-to-ask advice on how present question in clear, answerable way).
for example, above won't generate random value within arbitrary range; (exclusive) max value can ever power of 10. also, due way biginteger
works, if looking value specific number of binary digits, can using random.nextbytes()
method, passing resulting array (making modifications appropriate ensure e.g. positive values).
see c# random bigint generator more inspiration (i would've voted duplicate, seems description might looking different result what's requested in question).
Comments
Post a Comment