for a single dimensional array normally I would use a for loop like the one below but I can't think of a way to do this that doesn't involve lots of loops.
for (int i = 0; i < myArray.length; ++i)
{
myArray[i] = rnd.Next(1, 500);
}
for a single dimensional array normally I would use a for loop like the one below but I can't think of a way to do this that doesn't involve lots of loops.
for (int i = 0; i < myArray.length; ++i)
{
myArray[i] = rnd.Next(1, 500);
}
You can try low level Buffer.BlockCopy to conceal the loop(s):
// N-D array (whatever dimensions)
int[,,] array = new int[3, 5, 11];
Buffer.BlockCopy(
Enumerable
.Range(0, array.Length)
.Select(x => rand.Next(0, 500))
.ToArray(),
0,
array,
0,
array.Length * sizeof(int)); // sizeof(int) : we copy bytes...
we create 1-D array
Enumerable
.Range(0, array.Length)
.Select(x => rand.Next(0, 500))
.ToArray()
with the total length of N-D one (array.Length == array.Length(0) * ... * array.GetLEngth(N)
) and copy it into N-D one.
Use more than one loop:
for (int i = 0; i < myArray.Length; ++i)
{
for (int j = 0; j < myArray[i].Length; ++j)
{
myArray[i][j] = rand.Next(0, 500);
}
}