Pick random function help

Hello Grasshopper staff! I love your app cause I always wanted to learn how to program but couldn’t afford college to learn how.

Anyway, in the playground I’m trying to make a program that picks 3 random colors from a list of colors then draw a box of each color but pick random function only picks one random color from a list. Is there a way to get pick random to pick a random color a certain number of times?

P.S I’m only at operators in the app so maybe I haven’t learned something that can do this.

Hey there, great question!

pickRandom() will only return 1 value from a list of values, but you can try creating 3 variables, with each one calling pickRandom(). Then use drawBox() 3 times, once for each color.

Let me know how your project turns out!
Ben

Is there a shorter way to do the same thing? Maybe make a single variable with pickrandom from a list of colors then use a for loop to repeat it 3 times and each time draw a box of that color? BTW, I’m trying to make a program that when all three boxes are the same color the program prints a message.

Yes, there are lots of ways to achieve the same output. One good strategy is to think of a quick solution, then make small improvements to it (more readable, less calculations).

Using a loop as you suggested is a good idea if you plan on ever changing the number of boxes chosen.


As far as checking if all 3 boxes are the same color, the === can only compare 2 things at a time. But you could do:

  • (box1 === box2) && (box2 === box3)

Or, inside your loop, compare the new box to the previous one, and if there’s ever a mismatch, then you know all 3 can’t be the same color. This is a little tricky because you have to store the previous color, and also remember that your 1st box is never a mismatch because there’s no previous box.

–Frankie

Okay, that would work but I’m trying to make a program that if all 3 boxes equal a certain color print a message if all 3 of them isn’t that certain color print a different message. I thought I could use a for loop to run pickrandom(color) 3 times and each time draw a box then check if all three equal a certain color print a message if not print a different message.

P.S. Sorry if this post is too long. I just want to make sure you understand what I’m trying to do and If there is a shorter way to do it.

Here’s one way you can do it:

let color = ['red','blue']; // make a list of colors
let winningColor = 'red'; // store the "certain color"
let matches = 0; // make a variable to keep track of how many boxes match the certain color

for (let i = 0; i < 3; i++) {
    let randomColor = pickRandom(color); // pick a random color
    if (randomColor === winningColor) { // check if it matches the certain color
        matches++; // if it does, increment the number of matches
    }
    drawBox(randomColor); // draw the box
}

if (matches === 3) { // check if all 3 boxes matched
    print('win');
} else {
    print('lose');
}

Here’s another way:

let color = ['red','blue'];
let winner = 'red';

let c1 = pickRandom(color);
let c2 = pickRandom(color); 
let c3 = pickRandom(color);

drawBox(c1);
drawBox(c2);
drawBox(c3);

if (c1 === winner && c2 === winner && c3 === winner) {
    print('win');
} else {
    print('lose');
}

There isn’t an easy way to check for the drawBox colors that are already on the screen. You can either check them as you make them, or make a variable to store the color and make the box using that variable.

–Frankie

Thanks, the second one is what I did before. I couldn’t think of another way to code it. The first example is a very interesting way to code it.