-6

As the title says, I'm developing in C#.
I'd like to generate a number which has more than 9 numbers, so I thought about generating numbers, follow by 9.
For instance, if I want to generate a number which has 10 digits - I'd generate first a number which has 9 numbers, and then a number which has 1 digit.
If I generate the number 20, I'd generate first 2 numbers with 9 digits each, and then a number with 2 digits.
This is what I've tried:

for (int i = 0, j = (int.Parse(inputRandom.Text) % 9 == 0 ? int.Parse(inputRandom.Text) % 9 : int.Parse(inputRandom.Text) % 9 + 1); i < 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 Zero returns a string with 0, times the number specified in. I.e Zero(1) would return: "0"
I need this method because the method Next(Int32) can return a number, up to what specified between the brackets. I.e Next(10) would return a number between 0 and 9.

My goal

I want to generate a number with a number of digit above 9, and put the number into the string numToSend. I cannot simply do this, because the method Next() can only generate ints.

avi12
  • 157
  • 1
  • 2
  • 8
  • 1
    Clean up the mess you're calling "*code*" – Stefan Falk Mar 21 '15 at 10:36
  • And you're confusing "number" and "digit" a few times I guess. Formulate your question better. – Stefan Falk Mar 21 '15 at 10:37
  • Do you have a solution for what I'm asking? – avi12 Mar 21 '15 at 10:39
  • First we have to figure out what you're asking. Why do you have a method that always returns the string `"0"`? How is the goal here different from simply generating a random N-digit string? Please state exactly what your program input would look like, and what output you expect to get from that input. And of course, state a clear as you can the actual specification of the behavior you are trying to implement. – Peter Duniho Mar 21 '15 at 10:44
  • No because after reading a few lines and looking at your code I lost interest in understanding the problem and finding a solution but I took the time to hint you why people might vote down your question. – Stefan Falk Mar 21 '15 at 10:46
  • I update my main post. – avi12 Mar 21 '15 at 11:17

1 Answers1

0

Even after your edit, I don't find the question very clear. However, I do think I understand that you are essentially trying to generate a random BigInteger value, and that you want the random value to be within a range defined by some specific number of digits.

For example, if you ask for a 10-digit value, some value between 0 and 9999999999 should be returned. If you ask for a 20-digit value, some value between 0 and 99999999999999999999 should be returned.

Based on that understanding, I believe this method should accomplish what you are asking:

BigInteger NextRandom(Random random, int digitCount)
{
    BigInteger result = 0;

    while (digitCount-- > 0)
    {
        result = result * 10 + random.Next(10);
    }

    return result;
}

Note that the number returned could have fewer than digitCount actual digits in it. In fact, there's always a 1-in-10 chance of that happening. And a 1-in-100 chance of the result having fewer than (digitCount - 1) digits, etc.

If you want something different from that, please be more specific about the exact requirements of the output you want (read https://stackoverflow.com/help/how-to-ask for advice on how to present your question in a clear, answerable way).

For example, the above won't generate a random value within some arbitrary range; the (exclusive) max value can only ever be a power of 10. Also, due to the way BigInteger works, if you are looking for a value with a specific number of binary digits, you can do that simply by using the Random.NextBytes() method, passing the resulting array (making modifications as appropriate to ensure e.g. positive values).

See also C# A random BigInt generator for more inspiration (I would've voted as that for a duplicate, but it seems from your description that you might be looking for a slightly different result than what's requested in that question).

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136