mercredi 26 avril 2017

Generating 5 unique coordinates on a grid using 2 arrays

I am using Java to program an application that randomly generates 8 X points in a 5x5 grid.

Both the x and y axis have separate arrays I use to store the coordinates. These 2 arrays are parallel arrays that correlate with each other. The problem I am having is I can not seem to generate 8 unique coordinates.

For example, no matter what I do, I will eventually only have less than 8 unique coordinates because it so happens that the same coordinates are generated twice. I am trying to find an algorithm that can ensure the 8 random generated coordinates to be unique.

Also, For some reason, when I use the Random class to generate random numbers between 1 - 5, I sometimes get a 0 as a randomly generated number. This is how I use the Random class:

Random rand = new Random(System.currentTimeMillis()); coordinate = rand.nextInt(5)+1;

Thank you all for your time!




Aucun commentaire:

Enregistrer un commentaire