How to choose at least 4 images

edited February 2016 in Questions about Code

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

  • edited February 2016 Answer ✓

    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.

    listepieges(int nbre) {
    
      int x, y, img=0;    
      liste = new pieges[nbre];
    
      int compteur = 0;
      int i = 0;
      while (i < nbre || compteur < 4 || compteur > nbre - 4) {
        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
          compteur++;
        } else {
          img = 2; // On séléctionne les guêpes
        } 
    
        liste[i%nbre] = new pieges("image_"+img, x, y); // On stock les pièges dans un tableau.
        i++;
      }
    }
    

    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.

  • edited February 2016 Answer ✓

    I think koogs meant something like this:

    listepieges(int nbre) {
      int x, y, img=0;    
      liste = new pieges[nbre];
    
      for (int i = 0; i < nbre; i++) {      
        if (i < 4) {  // les 4 premières pièges ce sont des hérissons
          img = 1;
        } else if (i < 8) {  // les 4 pièges suivantes ce sont des guêpes
          img = 2;
        } else if (random(1) > 0.5) {  // les autres pièges sont choisis aléatoirement
          img = 1;
        } else {
          img = 2;
        }
    
        if (img == 1) {
           y = 10 * (int) random(20/10, 300/10)); // y(hérissons) entre 20 et 300 
        } else {
          y = 10 * (int) random(400/10, 580/10))  // y(guêpes) entre 400 et 580
        }
        x = 10 * int(random(20/10, 780/10));
    
        liste[i] = new pieges("image_"+img, x, y); // On stock les pièges dans un tableau.
      }
    }
    
Sign In or Register to comment.