c# 2 Dimension Array Random Numbers - c#

So I need to make 2 Dimension Array with random numbers, and expel the numbers like in multiplication table. It should look like this:
My code for now looks like this:
Random random = new Random();
int[,] array = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
array[i, j] = random.Next();
Console.Write("\t{0}\t{1}" ,i,j);
}
EDIT:::This worked for me!
Random random = new Random();
int[,] array = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
array[i, j] = random.Next(10);
Console.Write(array[i,j]+"\t");
}
Console.WriteLine("\n");

A 10x10 array of random numbers could be generated like this:
var rnd = new Random();
var randomArray = Enumerable.Range(0,10).Select(x => {
return Enumerable.Range(0,10).Select(y => rnd.Next()).ToArray();
}).ToArray();
Live example: http://rextester.com/GWTAK54347

As suggested by ThatBrianDude
static void Main(string[] args)
{
Random random = new Random();
int[,] array = new int[10, 10];
for (int i = 0;
i < 10;
i++)
{
for (int j = 0;
j < 10;
j++)
{
array[i,
j] = random.Next();
Console.Write("\t{0}",
array[i,
j]);
}
Console.Write("\n");
}
Console.ReadKey();
}

I think that this should work for you:
private static void RunArrayCode()
{
Random random = new Random();
int[,] array = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
array[i, j] = random.Next();
// No tabs if it's the first item in a row
Console.Write("{0}{1}\t{2}", (j == 0 ? "" : "\t"), i, j);
}
// Each new row goes on its own line
Console.WriteLine();
}
}

Related

How to dynamically create multiple arrays [5,5] , and fill them with random numbers

How to create and fill multiple arrays [5,5], and print it to console.
for now i have that, but i cant get how to dynamically create arrays.
incoming value - for example 3, its mean to create 3 arrays
and fill them with random values
int n = 5;
int count_array = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[n, n];
Random rnd = new Random();
for (var i = 0; i < n; i++)
{
for (var j = i; j < n; j++)
{
---
}
}
Like that?
int n = 5;
int count_array = Convert.ToInt32(Console.ReadLine());
int[,,] arr = new int[count_array, n, n];
Random rnd = new Random();
for (int current_array = 0; current_array < count_array; current_array++)
{
Console.WriteLine($"Populating array #{current_array}:");
for (var i = 0; i < n; i++)
{
for (var j = 0; j < n; j++)
{
int value = rnd.Next();
arr[current_array, i, j] = value;
Console.Write(value+(j==n-1?"\r\n":","));
}
}
}
Console.ReadLine();

how to remake 2d array random unique numbers as 3d?

static void Main(string[] args)
{
Random r = new Random();
int[,] x = new int[10,8];
int[] temp = new int[x.Length];
// two dimensional array and i want for three dimensional array
for(int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 100);
for(int j = 0; j < i; j++)
{
if(temp[i] == temp[j])
{
i--;
break;
}
}
}
for(int i = 0, index = 0; i < x.GetLength(0); i++)
{
for(int j = 0; j < x.GetLength(1); j++)
{
x[i, j] = temp[index++]; //two dimensional array unique numbers
Console.Write(x[i, j] + " ");
}
}
// i want do it as for 3D and 4 D array unique numbers like that method what can i change or add?
It's pretty straight forward to do what you're doing for higher dimensions.
Here's my code for 3D:
var r = new Random();
int [,,] x = new int[10, 8, 8];
var count =
Enumerable
.Range(0, x.Rank)
.Select(y => x.GetLength(y))
.Aggregate((y, z) => y * z);
var values =
Enumerable
.Range(10, count)
.OrderBy(y => r.Next())
.ToArray();
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
x[i, j, k] = values[v++];
To change it to 4D these lines change:
int [,,,] x = new int[10, 8, 8, 12];
// ...
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
for (var l = x.GetLowerBound(3); l <= x.GetUpperBound(3); l++)
x[i, j, k, l] = values[v++];
Now, in this code I have explicitly called GetLowerBound as well as GetUpperBound as it is possible in .NET code to have a non-zero based array.
Also, rather than repeatedly re-try getting random numbers until you have unique numbers I simply generated a sequence of unique numbers and then randomly sorted them. That's a little different from your original code. You needed 80 (10 x 8) random values and you were choosing from values ranging from 10 to 99 inclusive. So you had some holes in your numbers.
Random r = new Random();
int[,,] x = new int[10, 8, 8];
int[] temp = new int[x.Length];
#region one dimensional array unique numbers.
for (int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 650);
for (int j = 0; j < i; j++)
{
if (temp[i] == temp[j])
{
i--;
break;
}
}
}
#endregion
for (int i = 0, index = 0; i < x.GetLength(0); i++)
{
for (int j = 0; j < x.GetLength(1); j++)
{
for (int k = 0; k < x.GetLength(2); k++)
{
x[i, j, k] = temp[index++];
Console.Write(x[i, j, k] + " ");
}
Console.WriteLine();
}
}// i think it's correct code i've changed it

Random 2-D array with LINQ c#

How I can create and display random integer 2-D array with LINQ?
Help me please!
I create random 2-D array, but not with LINQ.
Random rnd = new Random();
int[,] matrix = new int[rows, columns];
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
matrix[i, j] = rnd.Next(-100,100);
Console.WriteLine("Array:");
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
Console.Write("{0}\t", matrix[i, j]);
Console.WriteLine();
}
Using linq to create a 10x10 array:
var r = new Random();
var result = Enumerable.Range(0, 10).Select(x =>
Enumerable.Range(0, 10).Select(y => r.Next()).ToArray())
.ToArray();
If you really want to over-complicate this:
static void Main(string[] args)
{
int rows = 10;
int columns = 10;
int[,] matrix = new int[rows, columns];
Random rnd = new Random();
Enumerable.Range(0, rows)
.ToList()
.ForEach(row => Enumerable.Range(0, columns)
.ToList()
.ForEach(column =>
{
matrix[row, column] = rnd.Next(-100, 100);
Console.Write(column == columns ? Environment.NewLine + matrix[row, column].ToString() + "\t" : matrix[row, column].ToString() + "\t");
}));
Console.ReadKey();
}

c# adding random numbers to array

I am making a array filled with random numbers in c# but I can't get it to work.
int[,] array = new int[10, 5];
int x, y;
x = 0;
y = 0;
while (y <= 5)
{
Random r = new Random();
int rand = r.Next(-50, 50);
array[x, y] = rand;
if (x == 10)
{
x = 0;
y++;
}
x++;
}
Use nested for loops, it is much easier:
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
array[i, j] = rnd.Next(-50, 50);
}
}
Your while loop is not readable but it's correct,except you should change while (y <= 5) to while (y < 5) otherwise you will get an IndexOutOfRangeException. And you should define your Random instance outside of the loop.
int[,] array = new int[10, 5];
int x = 0, y = 0;
Random r = new Random();
while (y < 5)
{
int rand = r.Next(-50, 50);
array [x, y] = rand;
x++;
if (x == 10)
{
x = 0;
y++;
}
}
Try this:
int[,] array = new int[10, 5];
Random rnd = new Random();
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 5; col++)
{
array[row, col] = rnd.Next(-50, 50);
}
}
you need to change yours if or array declaration to
int[,] array = new int[11, 6];
also there is other problem, you need to create random before while
Random r = new Random();
while (y <= 5)
{
to print out the values you can use for
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
Console.WriteLine(array[i, j]);
}
int[,] array = new int[10, 5];
int x, y;
x = 0;
y = 0;
while (y < 5)
{
Random r = new Random();
int rand = r.Next(-50, 50);
array[x, y] = rand;
if (x == 9)
{
x = 0;
y++;
}
x++;
}
Let's see if this works.

Confused about how to fill an array

I would like to fill out a 2-dimensional array randomly .But I dont know how to assign .This is what i have yet
Random rnd = new Random();
int x = rnd.Next(0, 3);
int y = rnd.Next(0, 3);
int[,] array=new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
?? I have no idea
}
}
try this.
Random rnd = new Random();
int[,] array=new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
array[i,j] = rnd.Next(0,100);
}
}
You can do it like this. But i wonder why you are generating 2 random numbers
Random rnd = new Random();
int x = rnd.Next(0, 3);
int y = rnd.Next(0, 3);
int[,] array=new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
array[i,j] = yourvalue;
}
}

Categories

Resources