1

I am trying to indirectly initialize an byte[,] by not specifying the bytes sicne the should be chosen randomly.

 byte[] First = BitConverter.GetBytes(rnd.Next(10000, 90000));
            byte[] Second = BitConverter.GetBytes(rnd.Next(10000, 90000));
            byte[] Third = BitConverter.GetBytes(rnd.Next(10000, 90000));
            byte[] Fourth = BitConverter.GetBytes(rnd.Next(10000, 90000));
            byte[] Fifth = BitConverter.GetBytes(rnd.Next(10000, 90000));
            byte[] Sixth = BitConverter.GetBytes(rnd.Next(10000, 90000));
            byte[] Seventh = BitConverter.GetBytes(rnd.Next(10000, 90000));
            byte[] Eighth = BitConverter.GetBytes(rnd.Next(10000, 90000));

            byte[,] Arr2D = new byte[,] { First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth };

This is how I planned to do it, but there is a problem: It's not possible to initialize a byte[,] like so.

And I cannot specify static values for the arrays, so creating nested arrays is not possible.

What would be the correct way to initialize a byte[,] with random values like shown above?

1
  • Maybe something like byte[,] Arr2D = new byte[,] { { First, Second}, {Third, Fourth} };? It's not clear what goes where in your example. Commented Jun 19, 2015 at 0:57

1 Answer 1

1

If I understand right your question maybe this will do the job:

var rnd = new Random();
var Arr2D = new byte[8,4]; // 8 rows and 4 (BitConverter.GetBytes give us an array of bytes with length 4). 
for (var i = 0; i < 8; i++)
{
    var sequence = BitConverter.GetBytes(rnd.Next(10000, 90000));
    for (int j = 0; j < sequence.Length; j++)
    {
        Arr2D[i, j] = sequence[j];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.