We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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();
}
Answers
edit post, highlight code, press ctrl-o to format.
Two very different behaviors that would both meet your criteria:
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.
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.
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:
Very glad you had an insight and are on the right track!
Note that this:
... is the same as this:
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 theif
AND theelse
comparisons -- they overlap.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 theelse
-- so don't check for the same value again in anelse.
Most importantly:
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: