# KeyPressed cannot show a random position to a ellipse

**URL:** https://discourse.processing.org/t/keypressed-cannot-show-a-random-position-to-a-ellipse/40665
**Category:** Beginners
**Tags:** homework
**Created:** [January 21, 2023, 4:10am UTC](https://discourse.processing.org/t/keypressed-cannot-show-a-random-position-to-a-ellipse/40665 "2023-01-21T04:10:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![carmavals](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@carmavals](https://discourse.processing.org/u/carmavals)
#### Post date: [January 21, 2023, 4:10am UTC](https://discourse.processing.org/t/keypressed-cannot-show-a-random-position-to-a-ellipse/40665/1 "2023-01-21T04:10:12Z")

</div>

Hello!

I have a question about a sketch. I am trying to draw a ellipse on a random position everytime a key is pressed in this case the key ‘a’. The situation is that it does draw the ellipse in a random position but it will draw it again in the same spot. I would like to know y i can make it draw in another spot evrytime the key ‘a’ is pressed. Here is my code:

```auto
tboolean a, q;
int x = int(random(20,2000));
int y = int(random(15, 1000));
void setup(){
size(2500, 1500);
colorMode(HSB, 360, 100,100);
background(0,0,0);
a = false;
q = false;

}

void draw(){
  if(q == true){
    
  for(int i = 0; i < 2501; i = i+250){
  
  strokeWeight(1);
 stroke(0, 0, 100, 5);
line(i, 0, i,1500 );

  for(int z = 0; z <1500; z= z+150){
    strokeWeight(1);
    stroke(0, 0, 100, 1);
  line(0, z, 2500,z );   
        
}
  
  }

  }

if(a == true){
  //int tono = int(random(299, 300));
  stroke(299, 99, 99, 30);
 strokeWeight(int(random(90,100)));
ellipse(x,y, 500, 500);

}
 

}

void keyPressed(){
if(key == 'a'){
  background(0,0,0);
  q = true;
a = true;
}

if(key=='q'){
  background(0,0,0);
  a = false;
q = true;

}

}

```

Thank you!

---

<div class="post-metadata">

### Author: ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)
#### Post date: [January 21, 2023, 6:30am UTC](https://discourse.processing.org/t/keypressed-cannot-show-a-random-position-to-a-ellipse/40665/2 "2023-01-21T06:30:31Z")

</div>

Where you are checking if ‘a’ is pressed you’ll want to generate new random numbers for x and y, they’re only getting generated as a global, Hope that helps

```auto
  if (key == 'a') {
    background(0, 0, 0);
    q = true;
    a = true;
    x = int(random(20, 2000)); // NEW RANDOM X
    y = int(random(20, 2000)); // NEW RANDOM Y
  }

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 21, 2023, 7:47am UTC](https://discourse.processing.org/t/keypressed-cannot-show-a-random-position-to-a-ellipse/40665/3 "2023-01-21T07:47:40Z")

</div>

> [@carmavals](#):
>
> ```auto
> if(a == true){
> //int tono = int(random(299, 300));
> stroke(299, 99, 99, 30);
> strokeWeight(int(random(90,100)));
> ellipse(x,y, 500, 500);
> 
> ```

Add a = false; here ?

Chrisir

---

<div class="post-metadata">

### Author: ![carmavals](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@carmavals](https://discourse.processing.org/u/carmavals)
#### Post date: [January 21, 2023, 3:03pm UTC](https://discourse.processing.org/t/keypressed-cannot-show-a-random-position-to-a-ellipse/40665/4 "2023-01-21T15:03:08Z")

</div>

Thank you! it did work for drawing ellipses on random positions on the screen, now my question continues for a one single ellipse that changes position every time the key ‘a’ is pressed.

---

<div class="post-metadata">

### Author: ![carmavals](https://avatars.discourse-cdn.com/v4/letter/c/da6949/32.png) [@carmavals](https://discourse.processing.org/u/carmavals)
#### Post date: [January 21, 2023, 3:08pm UTC](https://discourse.processing.org/t/keypressed-cannot-show-a-random-position-to-a-ellipse/40665/5 "2023-01-21T15:08:14Z")

</div>

I have a solution for my last question. I wrote on the conditional of the keyPressed function

x = int(random(25,2500));  
y = int(random(15,1500));

so it will look like this

```plaintext
if(key == 'a'){
  background(0,0,0);
  x = (int(random(20,2500)));
  y = (int(random(15, 1500)));
  q = true;
a = true;
}

```

---

<div class="post-metadata">

### Author: ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)
#### Post date: [January 22, 2023, 1:22am UTC](https://discourse.processing.org/t/keypressed-cannot-show-a-random-position-to-a-ellipse/40665/6 "2023-01-22T01:22:20Z")

</div>

Thats the solution I gave you lol
