# Random function and constrain

**URL:** <https://discourse.processing.org/t/random-function-and-constrain/20505>\
**Category:** Coding Questions\
**Created:** [May 5, 2020, 1:35pm UTC](https://discourse.processing.org/t/random-function-and-constrain/20505 "2020-05-05T13:35:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Filo96ge](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@Filo96ge](https://discourse.processing.org/u/Filo96ge)\
**Post date:** [May 5, 2020, 1:35pm UTC](https://discourse.processing.org/t/random-function-and-constrain/20505/1 "2020-05-05T13:35:19Z")

</div>

Hello everybody, I’m working on a small personal exercise and I cannot undestrand how to avoid the born of random ball inside of the small square.  
The idea is to fill only the white square and leave the pink-center square empty; I was thinking with map or constrain function but I never use it… Someone can help me?

CODE:

```auto
void setup () {
  size (1000, 1000);
  background (255);

  rectMode (CENTER);
  fill (255, 0, 0, 40);
  rect (width/2, height/2, 300, 300);
}

void draw() {
  float x = random (width);
  float y = random (height);
  fill(0,30);
  ellipse (x, y, 30, 30);
}

```

---

<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:** [May 5, 2020, 1:58pm UTC](https://discourse.processing.org/t/random-function-and-constrain/20505/2 "2020-05-05T13:58:22Z")

</div>

You can either test a new ball if it’s not inside the rect and only then display it

```auto

void setup () {
  size (1000, 1000);
  background (255);

  rectMode (CENTER);
  fill (255, 0, 0, 40);
  rect (width/2, height/2, 300, 300);
}

void draw() {
  float x = random (width);
  float y = random (height);

  if ( ! (x>width/2-150 && x<width/2+150)) { // **and the y-position of the ball???? How do you have to add this? The ! means logical NOT** 
    fill(0, 30);
    ellipse (x, y, 30, 30);
  }
}

```

OR you just make the random values in a way that it’s automatically outside the inner rectangle, which is a bit harder, since the allowed area is AROUND the forbidden inner rectangle… (especially to distribute the points evenly)

Chrisir

---

<div class="post-metadata">

**Author:** ![Filo96ge](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@Filo96ge](https://discourse.processing.org/u/Filo96ge)\
**Post date:** [May 6, 2020, 2:08pm UTC](https://discourse.processing.org/t/random-function-and-constrain/20505/3 "2020-05-06T14:08:36Z")

</div>

I don’t know how to manage the y position (height) in order to create the balls around the inner rectangle….

---

<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:** [May 6, 2020, 5:27pm UTC](https://discourse.processing.org/t/random-function-and-constrain/20505/4 "2020-05-06T17:27:21Z")

</div>

> [@Chrisir](#):
>
> ```auto
> if ( ! (x>width/2-150 && x<width/2+150)) { // **and the y-position of the ball???? How do you have to add this? The ! means logical NOT** 
>    
> 
> ```

It’s the same as with x

if ( ! (x\>width/2-150 && x\<width/2+150 &&  
y\>height/2+150 && y height/2 + 150) ) {

---

<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:** [May 14, 2020, 10:56pm UTC](https://discourse.processing.org/t/random-function-and-constrain/20505/5 "2020-05-14T22:56:50Z")

</div>

You may also be interested in this circle-rect collision detection demo:

> **[Collision Detection](http://www.jeffreythompson.org/collision-detection/circle-rect.php)**
>
> An online book about collision detection using Processing.
