We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello all! I have loaded 7 image in an array and create a function that chose and places that image ImagePosition(); . I use the function 7 times and I will like the image does not repeat, ex. 3.png is chosen first I will like that it is not coming back for the next 6 choice. Same thing for the other image 2,3,4,5,6 and 7.
My code at the moment look like that maybe I am not in the good pats to accomplish what I want. Thank in advance for information.
PImage [] icone = new PImage[6];
float im1;
float im2;
float x;
float y;
float s;
float s2;
int ra;
int rx;
int ry;
int rs;
void setup() {
frameRate(1);
size(1000, 1000);
imageMode(CENTER);
for (int i = 0; i < icone.length; i++) {
icone[i] = loadImage (i+".png");
}
}
void draw() {
background(200);
ImagePosition();
ImagePosition();
ImagePosition();
ImagePosition();
ImagePosition();
ImagePosition();
ImagePosition();
}
void ImagePosition() {
ra=int(random(6));
x=(random (0, 10))*100;
rx = round(x);
y=(random (0, 10))*100;
ry = round(y);
s=(random (1, 5))*100;
rs = round(s);
image(icone[ra], rx, ry, rs, rs);
if (s< 200 ) {
float i=0;
float rx2;
float ry2;
i=0;
rx2=rs;
ry2=rs;
while (i < 9) {
image(icone[ra], rx+rx2, ry+ry2, rs, rs);
i= random(0, 10);
float[] a = {-rs, 0, rs};
float rb= ( random(0, 2));
int rb2 = round(rb);
rx2= rx2+ a[rb2];
float[] b = {-rs, 0, rs};
float rby= ( random(0, 2));
int rb3 = round(rby);
ry2= ry2+ b[rb3];
}
}
}
Answers
Ah, the old "how do I generate a sequence of random numbers without any repeats?" question.
The answer is to have an array, where each element is one of the numbers you want. Then swap each index with another index at random.
For example:
Thanks for that! I have try replace line 43
with your solution
But I get: cannot convert from int[] to int.
there's a shuffle in the java standard library which is probably better than coding your own (no offence!).
it does use lists though.
http://www.tutorialspoint.com/java/util/collections_shuffle.htm
Thanks, it looks interesting, Maybe I can include it in the draw function , so it will be shuffling each time the programme run and in ImagePosition() I do ra= ra+1 or something like this.
How to I integrate this code into processing, just to let you know I am a beginner level in coding don't know that much.
@PhiLho's shuffle() implementation answer @ StackOverflow: :D
http://StackOverflow.com/questions/1519736/random-shuffling-of-an-array
Compatible w/ JavaScript Mode (PJS) too: :D
http://ProcessingJS.org/tools/processing-helper.html
This pumped up version can shuffle() any object array instead of primitive 1s: \m/
@GoToLoop I am trying your code, I just realize I have 7 image (0.png to 6.png) I try to change static final int ICONS = 6; but it does not load 6.png
The post increment operator
++
: https://Processing.org/reference/increment.html@
icons[i++] = loadImage(i + ".png")
increases the iterator i.So when it reaches loadImage(), it's 1 more than what it was inside
[]
.Just move it to the last i:
icons[i] = loadImage(i++ + ".png")
O:-)You can also shorten your image() statements: *-:)
I change to
icons[i] = loadImage(i++ + ".png")
all image got nulla for command has parts for counter initialisation, checking and incrementation. just use those. anything else is asking for trouble.
thanks for your help Koogs and Gotoloop,Now it work