0

I want to generate a random number And fill in matrix elements I want to get every part of the number separately And filling the parts of the matrix with the parts of the number For example random number = 12340; I want to get the numbers 1 , 2 , 3 , 4 , 0

int []c = new int[5];

c[0] = 1; c[1] = 2; c[2] = 3; c[3] =4; c[4] =0;

But I cannot get every part of the number separately

3 Answers3

0

Using Linq for positive integers

var random = new Random();
int count = 5;
int max = (int)Math.Pow(10, count);
int number = random.Next(0, max);
var list = number.ToString(new string('0', count)).Select(c => char.GetNumericValue(c));
var digits = list.ToArray();
Console.WriteLine(number + " => " + string.Join(" ", digits));

Output

32409 => 3 2 4 0 9

123 => 0 0 1 2 3

Explanation

We generate the max value that can fit in the array of count cells (+1).

For count = 5, we generate numbers between 0..99999 thanks to the Pow.

We convert it to a string and select each char converted in its numeric value.

The use of new string('0', count) ensure that we will have 5 digits no matter the generated number: for example 50 will be 00050 and all cells of the array will be filled to match the required size. But if the size of this array may be less than the max count to just have the real digits, it is not needed and will produces:

32409 => 3 2 4 0 9

123 => 1 2 3

To have the expected array we apply the convert method on the resulting query.

0

You could use string conversion for it (matrix) or numeric (matrix2):

        // Via string
        int rndNum = 12345;
        string rndNumStr = "" + rndNum;
        char[] matrix = rndNumStr.ToCharArray();

        Console.WriteLine("|" + string.Join("|", matrix) + "|");
        
        // Numerically
        int rnd = 12345;
        int maxDigits = 10;     // You could calc the max digits too
        int[] matrix2 = new int[maxDigits + 1];
        int p = 0;
        
        for (int i = maxDigits; i >= 0; i--) {
            double rs = rnd / Math.Pow(10, i);
            int fl = (int) Math.Floor(rs);
            if (fl > 0) {
                rnd = (int) (rnd - (fl * Math.Pow(10, i)));
                matrix2[maxDigits - i] = fl;
            }
            else
            {
                p = i;
            }
        }

        for (int i = 0; i <= maxDigits; i++)
        {
            if (i>p) Console.Write("|" + matrix2[i]);
        }

        Console.Write("|");
George
  • 1,224
  • 12
  • 21
0

If you want to the digits mathematically:

public static void Main(string[] args)
{
    int randomNumber = 12340; // however you get this 
    Console.WriteLine(randomNumber);

    List<byte> digits = new List<byte>();
    while (randomNumber > 10)
    {
        digits.Add((byte)(randomNumber % 10));
        randomNumber /= 10;
    }
    digits.Add((byte)randomNumber);
    digits.Reverse();

    for(int i=0; i<digits.Count; i++)
    {
        Console.WriteLine(i + ": " + digits[i]);
    }

    Console.Write("Press Enter to quit...");
    Console.ReadLine();
}

Output:

12340
0: 1
1: 2
2: 3
3: 4
4: 0
Press Enter to quit...
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40