# How to keep a randomised y position circle the same while running code

**URL:** <https://discourse.processing.org/t/how-to-keep-a-randomised-y-position-circle-the-same-while-running-code/32301>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [September 18, 2021, 6:29am UTC](https://discourse.processing.org/t/how-to-keep-a-randomised-y-position-circle-the-same-while-running-code/32301 "2021-09-18T06:29:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewCoder1](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@NewCoder1](https://discourse.processing.org/u/NewCoder1)\
**Post date:** [September 18, 2021, 6:29am UTC](https://discourse.processing.org/t/how-to-keep-a-randomised-y-position-circle-the-same-while-running-code/32301/1 "2021-09-18T06:29:54Z")

</div>

Hi everyone, I’m able to get the y position of the circle to be randomised however it doesn’t stay in one spot. It changes the y position of the circle between (25,240) while running the code. I was trying to get the circle in a random position every time I run the code rather than it changing while the code is running. This is my current code for it. All help will be highly appreciated!

int x = 30;  
int y = 60;  
float gunX = x+20;  
float gunY = y+20;  
float circleY;  
boolean bulletFlies=false;  
float bulletX = 20;  
float bulletY = 20;

void setup() {  
size(500, 500);  
circleY = random(25,480);  
}

void draw() {  
background(0);  
fill(255, 56, 99);  
ellipse(x, y, 60, 60);  
gunX = x+12;  
gunY = y;  
bullet();

fill(255);  
circle(450,random(circleY), 80);  
}

void keyPressed() {  
if (key == ‘s’) y = y + 8;  
else if (key == ‘w’) y = y - 8;  
else if (key == ’ ') {  
bulletFlies=true;  
bulletX = gunX+14;  
bulletY = gunY+4;  
}  
}

void bullet() {  
if (bulletFlies) {  
ellipse(bulletX, bulletY, 6, 6);  
bulletX = bulletX+5;  
}  
}

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 18, 2021, 6:41am UTC](https://discourse.processing.org/t/how-to-keep-a-randomised-y-position-circle-the-same-while-running-code/32301/2 "2021-09-18T06:41:24Z")

</div>

Hi,

Please format your code by selecting it and using the `</>` button in the editor bar.

The `draw()` function is running several times per second. So if you have a `random()` function there it will give different values at each call of `draw()` so several times per second.

`Setup()` on the other hand only run once so a `random()` call there will give you a random value every time you launch your program but if you store it in a variable, it sill stay the same during the full execution.

This is already what you are doing with:

```auto
circleY = random(25,480);

```

But then in `draw()`, you have:

```auto
circle(450, random(circleY), 80);

```

What you program is doing is:

1. Pick a value at random between 25 and 480and store it in `circleY`.
2. At every loop, pick a random value between 0 and `circleY`.

I suppose that’s second step is where you don’t want to pick another random value. Instead you want to use directly the `circleY` variable that already contain a random value:

```auto
circle(450, circleY, 80);

```

---

<div class="post-metadata">

**Author:** ![NewCoder1](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@NewCoder1](https://discourse.processing.org/u/NewCoder1)\
**Post date:** [September 19, 2021, 2:55am UTC](https://discourse.processing.org/t/how-to-keep-a-randomised-y-position-circle-the-same-while-running-code/32301/3 "2021-09-19T02:55:57Z")

</div>

Oh okay, thank you for helping I understand why it wasn’t working 🙂

---

<div class="post-metadata">

**Author:** ![jackyjoy123](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jackyjoy123](https://discourse.processing.org/u/jackyjoy123)\
**Post date:** [September 19, 2021, 12:16pm UTC](https://discourse.processing.org/t/how-to-keep-a-randomised-y-position-circle-the-same-while-running-code/32301/4 "2021-09-19T12:16:27Z")

</div>

thanks for the awesome information.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 10, 2021, 9:24pm UTC](https://discourse.processing.org/t/how-to-keep-a-randomised-y-position-circle-the-same-while-running-code/32301/5 "2021-10-10T21:24:53Z")

</div>



---

<div class="post-metadata">

**Author:** ![jackyjoy123](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jackyjoy123](https://discourse.processing.org/u/jackyjoy123)\
**Post date:** [March 14, 2022, 11:01am UTC](https://discourse.processing.org/t/how-to-keep-a-randomised-y-position-circle-the-same-while-running-code/32301/6 "2022-03-14T11:01:39Z")

</div>

> [@jackyjoy123](#):
>
> thanks for the awesome information.  
> [creditcardsupportx.com&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;creditcardsupportx Resources and Information.](https://creditcardsupportx.com/barnes-and-noble-barclay-mastercard)  
> [creditcardsupportx.com&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;creditcardsupportx Resources and Information.](https://creditcardsupportx.com/target-red-credit-card)

thanks for the awesome information.
