I am trying to generate random numbers 0-49. I want to draw the links in a grid as long as the random number is not 0. Every time I do get a 0, it is followed by another ten 0s (give or take). In my research, they said this would be the problem if I kept creating new instances of my Random, but I am not doing that. My code is as follows:
//Class begins
namespace Mascape
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Wall[, ,] walls = new Wall[2, 15, 10];
int mapWidth=15;
int mapHeight=10;
Random rand = new Random(); //Outside of any methods or loops.
int mapFullness;
//The following is in my Initialize() method
//i is horizontal vs. vertical link
//j is the x position of the link
//k is the y position of the link
mapFullness = 50;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < mapWidth; ++j)
{
for (int k = 0; k < mapHeight; ++k)
{
if (!(rand.Next(50) == 0))
{
walls[i, j, k].active = true; //Links are drawn if active
}
}
}
}
I am trying to draw a grid with randomly missing links. The objective is to have them spread out, appearing about one in 50. This is the actual result:
Any idea why the randomness is lacking? Let me know if you would like more info, please.
There should be more to this. See the following code copied from yours generate random as expected.
Random rand = new Random();
int mapWidth = 5;
int mapHeight = 10;
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < mapWidth; ++j)
{
for (int k = 0; k < mapHeight; ++k)
{
Console.WriteLine(rand.Next(50));
}
}
}
Related
The network produces 1 x N x K tensor, where N is number of pixel positions and K is number of classes, each value represents score for a class at given position.
Current code to retrieve best class affinity for each position is working, but it is terribly slow and takes x4 more time, than the network run itself.
private int[,] GetClasses(List<DisposableNamedOnnxValue> output)
{
Tensor<float> outTensor = output.First().AsTensor<float>();
int[,] classes = new int[frameWidth,frameHeight];
for (int i = 0; i < frameWidth; ++i)
{
for (int j = 0; j < frameHeight; ++j)
{
int finalClass = 0;
float finalClassScore = 0;
for (int k = 0; k < nClasses; ++k)
{
float score = outTensor[0, i * frameHeight + j, k];
if (score > finalClassScore)
{
finalClassScore = score;
finalClass = k;
}
}
classes[i, j] = finalClass;
}
}
return classes;
}
Is there a better, faster way of doing this in Microsoft.ML ?
The solution I went with was to add argmax layer to the initial keras model. Keras output single value through argmax.
I have a function that stores values inside some arrays. Every time the function is called, the arrays have to be cleared before putting items in them.
public static void UpdateAttackedPieces()
{
attackedSquares = new List<int>[2][];
for (int color = 0; color < 2; color++) attackedSquares[color] = new List<int>[64];
for (int color = 0; color < 2; color++) for (int i = 0; i < 64; i++) attackedSquares[color][i] = new List<int>();
checkAttackersRays = new List<int>[2][];
for (int color = 0; color < 2; color++) checkAttackersRays[color] = new List<int>[64];
for (int color = 0; color < 2; color++) for (int i = 0; i < 64; i++) checkAttackersRays[color][i] = new List<int>();
// Update arrays
}
(It is a chess program and the function is for updating pieces under attack).
attackedSquares is an array (one per color) of the 64 squares on the board which holds information about its attackers (so attackedSquares[0][4][2] is the third attacker on the fifth square for white, for example)
I've done some testing and about 35% of the time it takes to run the function is spent initializing the jagged arrays. Is there a faster way to do this?
Thanks to #Matthey Watson's comment, I was able to bring the time down from 6.5 seconds to less than 1 second with this change in the code:
public static void UpdateAttackedPieces()
{
for (int color = 0; color < 2; color++) for (int i = 0; i < 64; i++) attackedSquares[color][i].Clear();
for (int color = 0; color < 2; color++) for (int i = 0; i < 64; i++) checkAttackersRays[color][i].Clear();
// Update arrays
}
just started to learn programming and I need 2D array without duplicates. This code (well edited for 1D) worked just fine for 1D but doesn't for 2D and have no clue why.
Would be very happy if someone helped me. Thanks.
Random r = new Random();
int[,] array = new int[10, 8];
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = r.Next(10, 100);
for (int k = 0; k < i; k++)
{
for (int l = 0; l < j; l++)
{
if (array[i,j] == array[k,l])
{
i--;
j--;
break;
}
}
}
}
}
With the nested j loop you are filling the entire second dimension for each i, but in the k and l loops you are only checking the grid to the upper and left of current cell. You could place a number twice because you are not checking every previously filled location.
If we change the code to this:
for (int k = 0; k < array.GetLength(0); k++)
{
for (int l = 0; l < array.GetLength(1); l++)
{
if (i != k && j != l && array[i, j] == array[k, l])
{
i--;
j--;
break;
}
}
}
Then you eliminate that problem, but you very quickly find that you get a IndexOutOfRangeException because you're decrementing both i & j at the same time. That's not moving you to the previous value - it's jumping back a whole row and left one cell - and that's ultimately sending i or j to -1 and that's not good.
If you want to do it like you're attempting then you need to have a way to simply move back to the previously filled cell, regardless of the row or column you're on.
Try this:
for (int x = 0; x < array.GetLength(0) * array.GetLength(1); x++)
{
array[x % array.GetLength(0), x / array.GetLength(0)] = r.Next(10, 100);
for (int y = 0; y < x; y++)
{
if (array[x % array.GetLength(0), x / array.GetLength(0)] == array[y % array.GetLength(0), y / array.GetLength(0)])
{
x--;
break;
};
}
}
However, that's not very efficient. Try this instead:
var values = Enumerable.Range(10, 90).OrderBy(_ => r.Next()).ToArray();
for (int x = 0; x < array.GetLength(0) * array.GetLength(1); x++)
{
array[x % array.GetLength(0), x / array.GetLength(0)] = values[x];
}
So, first of all, that just seems inefficient. Not sure why you want to do it this way, but then again, don't know the reason. Looks like a programming assignment.
My guess, you need a sort of double break going on. When you break finding a match, you don't break out to the "k" for loop, so you continue looking for a match even though you found one. Try setting a boolean value to say it's found, then use that in the criteria for the for loop for k. That'll break it out and let you start over on the outside loops for i and j.
Even then, it won't work because you indescriminately subtracted i and j. So if you're on position 1,2 you will jump back to 0,1 rather than 1,2. So you need to subtract j, if it drops below 0, then subtract from i and add "array.GetLength(1)" to j.
This solution depends on HashSet's property of containing unique elements. It has an Add method that returns false when we try to add existing elements.
Random r = new Random();
int[,] array = new int[10, 8];
var usedValues = new HashSet<int>();
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
int uniqueValue;
while (true)
{
uniqueValue = r.Next(10, 100);
if (usedValues.Add(uniqueValue)) break; // It is unique indeed
// It is not unique, try again.
}
array[i, j] = uniqueValue;
}
}
The above solution is more suitable when the range of the acceptable unique values is large. In this specific case the range is very small (10-99), and the solution offered by #Enigmativity is preferable.
I have a function that works for vector inputs but I need to run this on a cube. My impulse is I need to slice a vector out of the cube and then run it over a double for loop.
This is the pseudo code:
public static double[,,] cubefunction(double[,,] input)
{
int N = input.GetLength(0);
var outputvector = new double[N,N,N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
outputvector[1:N,i,j] = vectorfunction(input[1:N,i,j]);
}
}
return outputvector;
}
Obviously, 1:N is matlab notation to grab the entire row. Is there an equivalent in C#? Or how would I go about this without triple looping?
I've been trying to convert a bitmap object to a matrix of int.
I drew the letter 'C' in paint on a blank white sheet and the program was supposed to initialize the arr in place (x,y) with '0' if the pixel in the Bitmap object in the same position (x,y) is white and correspondingly '1' if it were a black pixel.
I wrote the following code:
static void Main(string[] args)
{
Bitmap arie = new Bitmap(#"C:\Users\User\Desktop\letter.bmp");
object [,] arr = new object[arie.Width, arie.Height];
int min=1000,counter=1;
for (int i = 0; i < arr.GetLength(0) - 1; i++)
{
for (int j = 0; j < arr.GetLength(1) - 1; j++)
{
if (arie.GetPixel(i, j).ToArgb() == Color.White.ToArgb())
{
arr[i, j] = 0;
}
else
arr[i, j] = 1;
}
}
for (int i = 1; i < arr.GetLength(0) - 2; i++)
{
for (int j = 1; j < arr.GetLength(1) - 2; j++)
{
Console.Write(arr[i, j]);
}
Console.WriteLine();
}
}
at the end the letter 'C' that I drew came out like this:
http://teachers.web.cern.ch/teachers/archiv/hst2000/teaching/expt/wavesand/image39.gif
Can anyone seem to recognize the issue?
The main issue is that when you are outputting the array to the console, you are traversing it by columns, rather than rows. That is why your array appears to be rotated by 90 degrees.
If in the second looping portion (where you are outputting to the console), you swap the inner and outer loops, you should see better results.
I also agree with the other comments that:
1) If you want better performance, use the LockBits/UnlockBits method of accessing the Bitmap
2) Your loop indexing is out of whack
If you use x and y instead of i and j, it will help you recognize when you're making these kind of mistakes.
Try this:
static void Main(string[] args)
{
Bitmap arie = new Bitmap(#"C:\Users\User\Desktop\letter.bmp");
object [,] arr = new object[arie.Width, arie.Height];
int min=1000,counter=1;
for (int i = 0; i < arie.Width; i++)
{
for (int j = 0; j < arie.Height; j++)
{
if (arie.GetPixel(i, j).ToArgb() == Color.White.ToArgb())
{
arr[i, j] = 0;
}
else
arr[i, j] = 1;
}
}
for (int y = 0; y < arie.Height; y++)
{
for (int x = 0; x < arie.Width; x++)
{
Console.Write(arr[x, y]);
}
Console.WriteLine();
}
}