# Playing random sounds from folder as an Urn (no repeat untill all the files are done)

**URL:** <https://discourse.processing.org/t/playing-random-sounds-from-folder-as-an-urn-no-repeat-untill-all-the-files-are-done/10391>\
**Category:** Libraries\
**Created:** [April 17, 2019, 1:23pm UTC](https://discourse.processing.org/t/playing-random-sounds-from-folder-as-an-urn-no-repeat-untill-all-the-files-are-done/10391 "2019-04-17T13:23:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![o\_b](https://avatars.discourse-cdn.com/v4/letter/o/8dc957/32.png) [@o\_b](https://discourse.processing.org/u/o_b)\
**Post date:** [April 17, 2019, 1:23pm UTC](https://discourse.processing.org/t/playing-random-sounds-from-folder-as-an-urn-no-repeat-untill-all-the-files-are-done/10391/1 "2019-04-17T13:23:00Z")

</div>

Hi there,  
I’m pretty new to processing.  
For an audio installation, I need a processing sketch to play random sounds from the data folder every 5 seconds, indefinitely.  
For this example I have 10 mp3 files, but I’ll need to use it with at least 100 sounds in the future.  
The files are numbered from 0 to 9.

My problem is that I need the program to play all the files and not repeating any of them untill it played all of the 10 files.  
And more important, when it has played the 10 files in a unique random order, I need it to do it again with an other unique order of random number, and so on.  
In Max/MSP this randomness is called an ‘urn’, but I can’t find similar things with processing.

Maybe I should generate a list of 10 numbers from 0 to 9 in a random order, then play my files following that order, and repeating that every 10 files. But I have no idea how to do it.

I hope it make sense.

Thanks for your help !

Here is my code.

* * *

```auto
import ddf.minim.*;
AudioPlayer player;
Minim minim;
int rand;
long lastTime = 0;
PFont f;

void setup() {
  size(200,200);
  minim = new Minim(this);  
}
 
 
void draw() {

if ( millis() - lastTime > 5000 ) {
   lastTime = millis();
    
    rand = int(random(0, 9));
    player = minim.loadFile(nf(rand) + ".mp3");
    player.play();
    print(rand);
 
  }
}

```

---

<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:** [April 18, 2019, 1:16am UTC](https://discourse.processing.org/t/playing-random-sounds-from-folder-as-an-urn-no-repeat-untill-all-the-files-are-done/10391/2 "2019-04-18T01:16:50Z")

</div>

one way could be to shuffle a IntList

```auto
import ddf.minim.*;
AudioPlayer player;
Minim minim;

int rand;
long lastTime = 0;
PFont f;

int max = 10;
IntList songnums = new IntList();
int songidx = 0;

void setup() {
  size(200, 200);
  minim = new Minim(this); 
  for ( int i = 0; i<max; i++) songnums.append(i);
  songnums.shuffle();
  println(songnums);
}

void draw() {
  if ( millis() - lastTime > 5000 ) {
    lastTime = millis();
    rand = songnums.get(songidx);
    if ( songidx++ > max ) songidx = 0;
    String songfile = nf(rand) + ".mp3";
    println("load: "+songfile);
    player = minim.loadFile(songfile);
    player.play();
  }
}

void keyPressed() {
  if ( key == 'r' ) { // reshuffle
    songnums.shuffle();
    println(songnums);
  }
}

```

---

<div class="post-metadata">

**Author:** ![o\_b](https://avatars.discourse-cdn.com/v4/letter/o/8dc957/32.png) [@o\_b](https://discourse.processing.org/u/o_b)\
**Post date:** [April 18, 2019, 1:31pm UTC](https://discourse.processing.org/t/playing-random-sounds-from-folder-as-an-urn-no-repeat-untill-all-the-files-are-done/10391/3 "2019-04-18T13:31:40Z")

</div>

> [@kll](#):
>
> import ddf.minim.\*; AudioPlayer player; Minim minim; int rand; long lastTime = 0; PFont f; int max = 10; IntList songnums = new IntList(); int songidx = 0; void setup() { size(200, 200); minim = new Minim(this); for ( int i = 0; i\<max; i++) songnums.append(i); songnums.shuffle(); println(songnums); } void draw() { if ( millis() - lastTime \> 5000 ) { lastTime = millis(); rand = songnums.get(songidx); if ( songidx++ \> max ) songidx = 0; String songfile = nf(rand) + “.mp3”; println("load: "+songfile); player = minim.loadFile(songfile); player.play(); } } void keyPressed() { if ( key == ‘r’ ) { // reshuffle songnums.shuffle(); println(songnums); } }

thanks kll !  
The IntList works great ! But it stopped a the end of the first list.  
But how could I program a ‘reshuffle’, for example, every time it has played a number a files thats equal to `int max -1` ?

Thanks a lot !

---

<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:** [April 18, 2019, 1:57pm UTC](https://discourse.processing.org/t/playing-random-sounds-from-folder-as-an-urn-no-repeat-untill-all-the-files-are-done/10391/4 "2019-04-18T13:57:54Z")

</div>

sorry my fault, i not test as i not have the song files

```auto
    rand = songnums.get(songidx);
    songidx++;
    if ( songidx >= max ) songidx = 0;

```

should not have a  
_ArrayIndexOutOfBoundsException: Array index out of range: x_

* * *

for the reshuffle i made a key [r]  
but you can do a

```auto
songnums.shuffle();

```

whenever you like.

an counter depending reshuffle would be

```auto
int count,countmax = 12;
//...
    if ( count >= countmax ) { 
      count = 0;
      songidx = 0;
      songnums.shuffle(); 
      println(songnums);
    }

```

---

<div class="post-metadata">

**Author:** ![o\_b](https://avatars.discourse-cdn.com/v4/letter/o/8dc957/32.png) [@o\_b](https://discourse.processing.org/u/o_b)\
**Post date:** [June 26, 2019, 5:05pm UTC](https://discourse.processing.org/t/playing-random-sounds-from-folder-as-an-urn-no-repeat-untill-all-the-files-are-done/10391/5 "2019-06-26T17:05:38Z")

</div>

Hi kll,

I just realised that I never said thank you for you answer. I feel very ashamed. Your help were really important. Thanks again !
