Display random number of Images [HELP]

I want to write a program that allows me to display a random amount of images on 1 row. So let's say I have images of a boy and a girl. What I would like to do is display an image of that girl over and over again on a different X position (x+=35), and as soon as the random generator generates the image of a boy, I want it to stop. I have attached a picture to clarify my desired outcome.

desired outcome

Currently I have this as code:

int y;
int x;

void setup() {
  size(500, 500);
  family();
}

void draw() {
}

void family() {
  PImage gezin;
  PImage gezin1;
  PImage lois;
  float a = random(0, 2);
  int fotoNum = int(a);
  lois = loadImage("lois.png");
  gezin = loadImage("gezin"+fotoNum+".png");

  for (int x = 0; x <= random(0, width) && a < 1; x += 35) {
    image(lois, x, y);
    println("The X Position is: " + x);
    println("The RNG is: " + a);
  }

  if (a >= 1) {
    gezin1 = loadImage("gezin1.png");
    image(gezin1, x, y);
  }

  println("The RNG is: " + a);
  smooth();
}
Tagged:

Answers

  • Answer ✓

    edit post, highlight code, press ctrl-o to format.

  • Two very different behaviors that would both meet your criteria:

    1. Flip a coin (call random) for and update the random variable, then draw accordingly. While the answer is 0, if the answer is 0 draw girl, if is is 1, draw boy (and stop). That's a while loop. Two girls will be common; five girls will be very rare; ten girls will be amazingly rare, but possible.

    2. Pick a count integer between 0 and 5, for each number up to that count and draw that many girls, then draw a boy. Two girls and five girls will be equally likely; ten girls will be impossible.

    It looks like you probably want the second one based on your image...?

  • @jeremydouglass I want the first one, but I am having a hard time with it. How can I make my program so that when it is 1, my program draws just 1 boy and than stops entirely..?

  • please don't post duplicates.

  • @MYNAMEISJASON -- use a while loop.

    If you need to focus on that part, try starting with a very simple while loop example, like println "0" or println "1" -- then add in the image code. Also try looking up while() in the reference.

    • a coin can be 0 or 1. It is 0.
    • while the coin is still 0
      • randomly flip the coin
      • if the coin is 1
        • print 1
        • quit
      • if the coin is 0
        • print 0
        • continue

    Half of your results should be 1, a quarter will be 01, and eighth 001, a sixteenth 0001, and about one out of every 32 results should be 00001 etc. In theory, you could get 00000000000000001 but increasingly high runs of zeros would be like winning the lottery or getting hit by an asteroid - possible, but very unlikely.

  • @jeremydouglass Hey man! Your comment about the coin sparked a light in me! I now completed the exercise! I looked at it all wrong and was stuck in this tunnel vision for quite some time now.

    After reading the coin comparison I almost instantly knew how to do it. So I made a for loop with a boolean expression that said for (int i = 0; i <=1;) Stating i as a random number between 0 and 2, this made me exit the loop as soon as the random number generator, generated number greater than 1 !!!! Which was exactly what I needed in order to complete the exercise. Thanks Jeremy! See here part of the code:

      for (float i = 0; i <= 1; ) {
        i = random(0, 2);
        println("a < 1        " + i);
        if (i <= 1) {
          image(lois, x, y);
          x+=35;
        } else if (i >= 1){
          image(petergriffin, x, y);
        }
      }
    
  • edited January 2018

    Very glad you had an insight and are on the right track!

    Note that this:

      i = random(0, 2);
      if (i <= 1) {
        print('A');
      } else if (i >= 1){
        print('B');
      }
    

    ... is the same as this:

      if (1 < random(0, 2)) {
        print('A');
      } else {
        print('B');
      }
    

    You do not need to say "If it is heads, do this, otherwise, if it is tails, do that." You can just say "If it is heads, do this, otherwise, do that."

    Also:

    Even if you needed two conditions (and you don't), do not need = in both the if AND the else comparisons -- they overlap.

      if (i <= 1) {
        // ...
      } else if (i >= 1){
        // ...
      }
    

    Imagine that i = 1. So the first clause is true. Will the "else" ever need to check for 1? No -- if the value is 1, then the if activated, not the else -- so don't check for the same value again in an else.

    Most importantly:

    for (float i = 0; i <= 1; ) {
      i = random(0, 2);
      // do something
    }
    

    This is legal code, and it works, but it would be confusing to many programmers -- modifying the for loop variable inside the loop is not a normal way of doing what you are trying to do.

    Read the reference for while(), in which the conditional term is expected to be modified in the loop, as you have done:

    Controls a sequence of repetitions. The while structure executes a series of statements continuously while the expression is true. The expression must be updated during the repetitions or the program will never "break out" of while. - https://processing.org/reference/while.html

    float i = 0;
    while (i < 0.5) {
      // do something
      i = random(0, 1);
    } 
    
Sign In or Register to comment.