193k views
1 vote
Which of the following programs correctly draws a rectangle at a random x position? (Select two)

Which of the following programs correctly draws a rectangle at a random x position-example-1

1 Answer

3 votes

Final answer:

The correct answer is option A and option C. To draw a rectangle at a random x position, the program should use a random number generator. Here is an example program.

Step-by-step explanation:

The correct answer is option A and option C.

To draw a rectangle at a random x position, the program should use a random number generator to generate the x-coordinate of the rectangle. One approach could be to use the random() function to generate a random value between 0 and the width of the canvas. The program can then use this random value as the x-coordinate of the rectangle.

Here is an example of a program that draws a rectangle at a random x position:

int canvasWidth = 800;
int canvasHeight = 600;

void setup() {
size(canvasWidth, canvasHeight);
}

void draw() {
background(255);
fill(0);
int x = int(random(0, canvasWidth));
rect(x, 0, 100, 100);
}

In this program, the random() function is used to generate a random value between 0 and the canvas width (800 in this case). The generated value is stored in the variable x, which is then used as the x-coordinate of the rectangle.

User Knaak
by
8.0k points