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.
Related
I am creating a function to generate random values for a multidimensional array and I get the error "method must have a return type" and "; expected".
Here is my code:
public static Double[,] X Generate_random()
{
Random rnd = new Random();
X = new Double[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
X[i, j] = rnd.Next(0, 10);
}
}
}
I think you meant to write your code like this
public static Double[,] Gerar_Aleatorio() {
Random rnd = new Random();
Double [,] x;
x = new Double[3, 3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
x[i, j] = rnd.Next(0, 10);
}
}
return x;
}
When you declare your return type for your method (in this case, Double[,]), you need to ensure that all paths through the method will end up returning an instance of that type. This is done using the return statement, which you would just place at the end of the method:
return X;
However, you never explicitly declare what type X is. When defining a variable, you must do: [Type] [VariableName] and additionally you can do the assignment on the same line (just as you did for Random). So your X definition should look like:
Double[,] X = new Double[3, 3];
Additionally, you have a typo (I assume) in the signature, where you have an X in the signature next to the type declaration. That should be removed, so your signature looks like:
public static Double[,] Generate_random()
Putting that all together, you have:
public static Double[,] Generate_random()
{
Random rnd = new Random();
Double[,] X = new Double[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
X[i, j] = rnd.Next(0, 10);
}
}
return X;
}
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();
}
}
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
Below is the code I wrote for clustering using genetic algorithm. Points are from a picturebox, generated randomly (X,Y) before calling this class. However, the result of this algorithm is much worse than k-means or lbg I'm comparing it to. Can someone take a look for any errors in the algorithm, maybe I omitted something. Thanks.
I did this using arrays, the 2 other I did using lists, but I don't think that should have any impact on result.
public class geneticAlgorithm
{
static int pom = 0;
static PictureBox pb1;
public geneticAlgorithm(PictureBox pb)
{
pb1 = pb;
}
public static void doGA(PointCollection points, int clusterCounter)
//points is a list of points,
//those point have (X,Y) coordinates generated randomly from pictureBox
//coordinates. clusterCounter is how many clusters I want to divide the points into
{
//this part converts list of points into array testtab,
//where each array field hold X,Y of a point
Point[] test = new Point[points.Count];
test = points.ToArray();
double[][] testtab = new double[test.Length][];
for (int i = 0; i < testtab.GetLength(0); i++)
{
testtab[i] = new double[2];
testtab[i][0] = test[i].X;
testtab[i][1] = test[i].Y;
}
//end of converting
int n = testtab.GetLength(0);
int k = clusterCounter;
int chromosomeCount = 500;
int dimensions = 2;
double[][] testClusters = new double[k][];
for (int i = 0; i < k; i++)
{
testClusters[i] = new double[dimensions];
}
double[][] testChromosomes = new double[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
testChromosomes[i] = new double[2 * k];
}
int[][] testChromosomesInt = new int[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
testChromosomesInt[i] = new int[2 * k];
}
double[] partner = new double[chromosomeCount];
double[][] roulette = new double[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
roulette[i] = new double[1];
}
double[][] errors = new double[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
errors[i] = new double[1];
}
double crossingPossibility = 0.01;
double mutationPossibility = 0.0001;
int maxIterations = 10000;
//here I create chromosomes and initial clusters
for (int j = 0; j < chromosomeCount; j++)
{
for (int i = 0; i < k; i++)
{
Random rnd = new Random();
int r = rnd.Next(n);
for (int q = 0; q < dimensions; q++)
{
testClusters[i][q] = testtab[r][q];
}
}
int help = 0;
for (int i = 0; i < k; i++)
for (int l = 0; l < dimensions; l++) // here is creation of chromosome
{
testChromosomes[j][help] = testClusters[i][l];
help++;
}
//end
//here I call accomodation function to see which of them are good
errors[j][0] = accomodationFunction(testClusters, testtab, n, k);
//end
//cleaning of the cluster table
testClusters = new double[k][];
for (int i = 0; i < k; i++)
{
testClusters[i] = new double[dimensions];
}
}
//end
for (int counter = 0; counter < maxIterations; counter++)
{
//start of the roulette
double s = 0.0;
for (int i = 0; i < chromosomeCount; i++)
s += errors[i][0];
for (int i = 0; i < chromosomeCount; i++)
errors[i][0] = chromosomeCount * errors[i][0] / s;
int idx = 0;
for (int i = 0; i < chromosomeCount; i++)
for (int j = 0; i < errors[i][0] && idx < chromosomeCount; j++)
{
roulette[idx++][0] = i;
}
double[][] newTab = new double[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
newTab[i] = new double[2 * k];
}
Random rnd = new Random();
for (int i = 0; i < chromosomeCount; i++)
{
int q = rnd.Next(chromosomeCount);
newTab[i] = testChromosomes[(int)roulette[q][0]];
}
testChromosomes = newTab;
//end of roulette
//start of crossing chromosomes
for (int i = 0; i < chromosomeCount; i++)
partner[i] = (rnd.NextDouble() < crossingPossibility + 1) ? rnd.Next(chromosomeCount) : -1;
for (int i = 0; i < chromosomeCount; i++)
if (partner[i] != -1)
testChromosomes[i] = crossing(testChromosomes[i], testChromosomes[(int)partner[i]], rnd.Next(2 * k), k);
//end of crossing
//converting double to int
for (int i = 0; i < chromosomeCount; i++)
for (int j = 0; j < 2 * k; j++)
testChromosomes[i][j] = (int)Math.Round(testChromosomes[i][j]);
//end of conversion
//start of mutation
for (int i = 0; i < chromosomeCount; i++)
if (rnd.NextDouble() < mutationPossibility + 1)
testChromosomesInt[i] = mutation(testChromosomesInt[i], rnd.Next(k * 2), rnd.Next(10));
//end of mutation
}
//painting of the found centre on the picture box
int centrum = max(errors, chromosomeCount);
Graphics g = pb1.CreateGraphics();
SolidBrush brush = new SolidBrush(Color.Red);
for (int i = 0; i < 2 * k - 1; i += 2)
{
g.FillRectangle(brush, testChromosomesInt[centrum][i], testChromosomesInt[centrum][i + 1], 20, 20);
}
return;
}
//end of painting
public static int max(double[][] tab, int chromosomeCount)
{
double max = 0;
int k = 0;
for (int i = 0; i < chromosomeCount; i++)
{
if (max < tab[i][0])
{
max = tab[i][0];
k = i;
}
}
return k;
}
public static int[] mutation(int[] tab, int elem, int bit)
{
int mask = 1;
mask <<= bit;
tab[elem] = tab[elem] ^ mask;
return tab;
}
public static double[] crossing(double[] tab, double[] tab2, int p, int k)
{
double[] hold = new double[2 * k];
for (int i = 0; i < p; i++)
hold[i] = tab[i];
for (int i = p; i < 2 * k; i++)
hold[i] = tab2[i];
return hold;
}
//accomodation function, checks to which centre which point belongs based on distance
private static double accomodationFunction(double[][] klastry, double[][] testtab, int n, int k)
{
double Error = 0;
for (int i = 0; i < n; i++)
{
double min = 0;
int ktory = 0;
for (int j = 0; j < k; j++)
{
double dist = distance(klastry[j], testtab[i]);
if (j == 0)
{
min = dist;
}
if (min > dist)
{
min = dist;
ktory = j;
}
}
Error += min;
}
pom++;
return 1 / Error;
}
public static double distance(double[] tab, double[] tab2)
{
double dist = 0.0;
for (int i = 0; i < tab.GetLength(0); i++)
dist += Math.Pow(tab[i] - tab2[i], 2);
return dist;
}
}
The algorithm should work like so: (excuse me if not the best english)
1. Get random points (let's say 100)
2. Check into how many clusters we want to split them (the same thing I would do using k-means for example
3. Get starting population of chromosomes
4. Throu cutting on the roulette, crossing and mutation pick the best x centres, where x is the amount of clusters we want
5. Paint them.
Here are some results, and why I think it's wrong: (it's using 100 points, 5 clusters)
k-means:
lbg:
genetic(without colors now):
I hope this clarifies a bit.
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;
}
}