# Sound and Random Shape Typing

**URL:** <https://discourse.processing.org/t/sound-and-random-shape-typing/6877>\
**Category:** Libraries\
**Created:** [December 24, 2018, 2:21pm UTC](https://discourse.processing.org/t/sound-and-random-shape-typing/6877 "2018-12-24T14:21:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vale](https://avatars.discourse-cdn.com/v4/letter/v/9de053/32.png) [@vale](https://discourse.processing.org/u/vale)\
**Post date:** [December 24, 2018, 2:21pm UTC](https://discourse.processing.org/t/sound-and-random-shape-typing/6877/1 "2018-12-24T14:21:23Z")

</div>

Hi everyone, I am editing an example of the Processing sound library. I would like that each time that a key has pressed an ellipse will be generated in a random position.  
I am stuck because the circle is generated randomly only when a first key is pressed then is always the same because the if condition is related to “validKey” but I don’t know how I could solve it.  
Thanks in advance!

```auto
import processing.sound.*;
SoundFile[] file;

int numsounds = 5;

float circleX;
float circleY;

void setup() {
  size(640, 360);
  background(0);
  circleX= random(width);
  circleY= random(height);
  
  // Create a Sound renderer and an array of empty soundfiles
  file = new SoundFile[numsounds];

  // Load 5 soundfiles from a folder in a for loop. By naming
  // the files 1.aif, 2.aif, 3.aif, ..., n.aif it is easy to iterate
  // through the folder and load all files in one line of code.
  for (int i = 0; i < numsounds; i++) {
    file[i] = new SoundFile(this, (i+1) + ".aif");
  }
}

void draw() {
}

void keyPressed() {
  // We use a boolean helper variable to determine whether one of the branches
  // of the switch-statement was activated or not
  boolean validKey = true;

  switch(key) {
  case 'a':
    file[0].play(0.5, 1.0);
    break;

  case 's':
    file[1].play(0.5, 1.0);
    break;

  case 'd':
    file[2].play(0.5, 1.0);
    break;

  case 'f':
    file[3].play(0.5, 1.0);
    break;

  case 'g':
    file[4].play(0.5, 1.0);
    break;

  case 'h':
    file[0].play(1.0, 1.0);
    break;

  case 'j':
    file[1].play(1.0, 1.0);
    break;

  case 'k':
    file[2].play(1.0, 1.0);
    break;

  case 'l':
    file[3].play(1.0, 1.0);
    break;

  case ';':
    file[4].play(1.0, 1.0);
    break;

  case '\'':
    file[0].play(2.0, 1.0);
    break;

  case 'q':
    file[1].play(2.0, 1.0);
    break;

  case 'w':
    file[2].play(2.0, 1.0);
    break;    

  case 'e':
    file[3].play(2.0, 1.0);
    break;

  case 'r':
    file[4].play(2.0, 1.0);
    break; 

  case 't':
    file[0].play(3.0, 1.0);
    break;

  case 'y':
    file[1].play(3.0, 1.0);
    break;

  case 'u':
    file[2].play(3.0, 1.0);
    break;    

  case 'i':
    file[3].play(3.0, 1.0);
    break;

  case 'o':
    file[4].play(3.0, 1.0);
    break;

  case 'p':
    file[0].play(4.0, 1.0);
    break;    

  case 'z':
    file[1].play(4.0, 1.0);
    break;

    // no valid key was pressed, store that information
  default:
    validKey = false;
  }
  
    if (validKey) {
      stroke(255);
      noFill();
      ellipse(circleX, circleY, 55, 55);
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 24, 2018, 2:42pm UTC](https://discourse.processing.org/t/sound-and-random-shape-typing/6877/2 "2018-12-24T14:42:12Z")

</div>

looks like you want the random position each time before you draw the circle??

```auto
//https://discourse.processing.org/t/sound-and-random-shape-typing/6877
float circleX;
float circleY;

void setup() {
  size(640, 360);
  background(0);
}

void draw() {
}

void keyPressed() {
  // We use a boolean helper variable to determine whether one of the branches
  // of the switch-statement was activated or not
  boolean validKey = true;

  switch(key) {
  case 'z':
    // only valid key for test
    break;

    // no valid key was pressed, store that information
  default:
    validKey = false;
  }

  if (validKey) {
    stroke(255);
    noFill();
    circleX= random(width); // _______________________________ add here
    circleY= random(height);
    ellipse(circleX, circleY, 55, 55);
  }
}

```

you understand that i cut down your code to the problem,  
as the full code can only be run with library and sound files…  
so help next time and do like this,  
it is easy to read and easy to test…

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 24, 2018, 4:35pm UTC](https://discourse.processing.org/t/sound-and-random-shape-typing/6877/3 "2018-12-24T16:35:11Z")

</div>

The problem is simple. You are using `circleX` and `circleY` as your “random” position, but the problem is that both are equal to the same single value you set to them in `setup()`. Every time you call `ellipse(circleX, circleY, 55, 55);` there is absolutely nothing to change `circleX` and `circleY`, so they keep their same values every time you run that function.

To do the thing right, what you’d want to do is to copy the `circleX= random(width);` and `circleY= random(height);` part of `setup()` right before the `ellipse(...` function, so that every time `ellipse(...` is called is also the same time `circleX` and `circleY` change to new random position, so it gets into that new random position every time.

However, the best approach with how your code is formed is to completely get rid of `circleX` and `circleY` values by deleting any lines of code related to them, and just using `ellipse(random(width), random(height), 55, 55);` instead, as there is no purpose to remember that random value.

Also, for fun, you could also use `fill(random(255), random(255), random(255));` instead of `noFill();` for random colors! 😃

---

<div class="post-metadata">

**Author:** ![vale](https://avatars.discourse-cdn.com/v4/letter/v/9de053/32.png) [@vale](https://discourse.processing.org/u/vale)\
**Post date:** [December 24, 2018, 6:35pm UTC](https://discourse.processing.org/t/sound-and-random-shape-typing/6877/4 "2018-12-24T18:35:13Z")

</div>

sorry for the uncutted code transcription!  
Anyway @Architector_4 has totally solved the problem, thank you all!!
