We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
Can someone help me?
Here my programm
listepieges(int nbre) {
int x,y,img=0;
liste = new pieges[nbre];
for (int i=0; i<nbre; i++) {
x = 10 * int(random(20/10,780/10));
while ((y = 10 * (int) random(20/10,580/10)) >= 300 & y < 400); //y prend des valeurs aléatoirement entre 20 et 580 exclus des valeurs entre 300 et 400
if (y<300) {
img = 1; // On séléctionne les hérissons
}
else {
img = 2; // On séléctionne les guêpes
}
liste[i] = new pieges("image_"+img,x,y); // On stock les pièges dans un tableau.
}
}
I would like to see at least 4 times image_1 minimum and 4 times image_2 minimum. In fact, when nbre = 20, the list stock 20 images randomly thanks to ramdom(). But I went at least 4 image_1 and 4 image_2 displayed on my program. After the 12 images that remain the images are chosen randomly.
Thanks you
Answers
You can use a counter that increases each time you choose image 1.
If you need to choose image 1 at least 4 times and image 2 at least 4 times, then the counter has to be between 4 and 16.
You can replace your for loop for a while loop and check if the counter has a valid value.
It will loop at least until it fills the list
(while i < nbre)
and then it will continue to loop while the counter has an invalid value(compteur < 4 || compteur > nbre - 4)
overwriting the existing values(liste[i%nbre])
.I think it might be easier to chose the random position based on the type of image rather than choosing the type of image based on the random position, given that you now want certain numbers of each.
Thank you very much BarbaraAlmeida, I understood what you have done. And thanks to the advice given by koogs. But in fact I want each image to be positioned on a well defined area with a minimum number of different traps to display. So I prefer the soluce of Barbara.
I think koogs meant something like this: