yes, you can make random something, and check if there was one already and try again.
or you find a way where there is
possible.
a random sequence to set N
- make a list ( 0 … n )
- shuffled
- use the first N of it
int items = 7; // to be placed randomly unique on a grid
int cols = 5;
int rows = 5;
int many = cols*rows;
IntList seq = new IntList(many);
int current=0;
boolean[][] myArr = new boolean[cols][rows];
void preset() {
for (int i = 0; i<many; i++) seq.append(i);
seq.shuffle();
for (int i = 0; i<many; i++) println(" i "+i+" "+seq.get(i));
}
void reset() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
myArr[i][j] = false;
}
}
}
void setitem(int rec) {
for (int i = 0; i < cols; i++)
for (int j = 0; j < rows; j++)
if ( seq.get(j+i*rows) == rec ) myArr[i][j] = true; //_ set acc shuffled list
}
void setitems(int item) {
for ( int i = 0; i < item; i++ ) setitem(i);
}
void checkitem() {
for (int i = 0; i < cols; i++)
for (int j = 0; j < rows; j++)
if ( myArr[i][j] ) {
println("item at i: "+i+", j: "+j);
// do what ever you want do here
}
}
void draw_grid() {
int x0 = 10, y0 = x0, w = 34, h = w, off = 2;
stroke(0);
for (int i = 0; i < cols; i++)
for (int j = 0; j < rows; j++) {
fill(0, 200, 0);
if ( myArr[i][j] ) fill(0, 0, 200);
rect(x0+i*(w+off), y0+j*(h+off), w, h);
}
}
void setup() {
size(200, 200);
reset(); //_____________________________ create empty array
preset(); //_____________________________ create a shuffled list
setitems(items); //______________________ set items
checkitem(); //__________________________ check what is set
}
void draw() {
background(200, 200, 0);
draw_grid(); //__________________________ green rect on yellow background w. blue items
}
but that was only half of your problem?
and actually i used the memory in the shuffled list
and translated into the memory in the grid, what a waste…
SAME SAME BUT DIFFERENT
// https://discourse.processing.org/t/i-need-random-circles-and-rectangles-in-a-grid-stuck-in-a-problem-i-cant-solve/15061/3
// rev 02 change from i , j to i .. many
// erase the array
int cols = 4, rows = cols, many = cols*rows; //_________________________ GRID layout
int x0 = 10, y0 = x0, w = 0, h = w, off = 2; //_________________________ grid rect layout ( auto size see setup )
int itemsb = 3; //______________________________________________________ BLUE items to be placed randomly unique on a grid
IntList seqb = new IntList(many); //____________________________________ BLUE list
boolean diagp = true; //________________________________________________ print
void presetb() {
for (int i = 0; i < many; i++) seqb.append(i); //_____________________ make a list of numbers 0 .. many-1
seqb.shuffle(); //____________________________________________________ and shuffle them
for (int i = many-1; i >= itemsb; i--) seqb.remove(i); //_____________ keep only as many we need
seqb.sort(); //_______________________________________________________ optional sort in draw order
}
void diag_print() {
if ( diagp ) {
println("BLUE items: "+itemsb);
for (int i = 0; i < seqb.size(); i++) println("BLUE i "+i+" "+seqb.get(i));
}
}
void draw_grid() {
stroke(0);
for (int i = 0; i < many; i++) {
fill(0, 200, 0);
if ( seqb.hasValue(i) ) fill(0, 0, 200);
rect(x0+(i%cols)*(w+off), y0+(floor(i/cols))*(h+off), w, h); //_____ grid math
}
}
void setup() {
size(200, 200);
w = ( width - 2*x0 ) /cols - off; //__________________________________ auto size rects
h = ( height -2*y0 ) /rows - off;
presetb(); //_________________________________________________________ create a shuffled list BLUE
diag_print();
}
void draw() {
background(200, 200, 0);
draw_grid(); //_______________________________________________________ green rect on yellow background w. BLUE items
}
please note how easy some things get when using IntList