# Randomly select file to preload (p5.js)

**URL:** https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148
**Category:** Coding Questions
**Created:** [July 10, 2021, 9:55am UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148 "2021-07-10T09:55:29Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![fleshcircuit](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@fleshcircuit](https://discourse.processing.org/u/fleshcircuit)
#### Post date: [July 10, 2021, 9:55am UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/1 "2021-07-10T09:55:29Z")

</div>

I am working on a project where I am using data from a json file, but I am wondering if it is possible to randomly select a file from the data folder for a sketch to use. I don’t want to load all the files, since I fear that will slow down the sketch. In my case, I will have 90 different json files in the data folder, but I want only one selected at random to preload and use in the sketch.

---

<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: [July 10, 2021, 12:00pm UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/2 "2021-07-10T12:00:38Z")

</div>

when you know the names in forehand just make a list

```auto
String[] list1 = {
"name1",
"name2",
....
};

```

and then create a random number and load the file indicated in the list1 at the position of the number index

Chrisir

P.S.

in standard processing you’d use file.list() but I don’t know the equivalent in p5.js

```auto
StringList inventory;  

//---------------------------------------------------------------------------
// processing core functions 

void setup() {
  size(880, 800);
  background(111); // gray
}

void draw() {
  background(111); // gray
  updateList(); 
  showList();
}

//---------------------------------------------------------------------------
// Other functions 

void updateList() {
  File file = new File(dataPath(""));
  String[] fileList = file.list();

  if (fileList==null) {
    println("Folder is empty.");
  } else {
    // success
    inventory = new StringList();
    for (String s1 : fileList) { 
      inventory.append(s1);
    }

    inventory.sort();
  }//else
}//func 

void showList() {
  if (inventory==null) 
    return; // leave 

  int i=0; 
  for (String s1 : inventory) { 
    text(s1, 33, 44+i*22);
    i++;
  }//for
  //
}//func
//

```

---

<div class="post-metadata">

### Author: ![fleshcircuit](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@fleshcircuit](https://discourse.processing.org/u/fleshcircuit)
#### Post date: [July 14, 2021, 9:47am UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/3 "2021-07-14T09:47:15Z")

</div>

Thanks. I created a txt file with the file names, which I preloaded as a string. I then ran into trouble trying to then use that to determine what JSON file to load.

```auto
let heartRate;
let list1;

function preload(){

  list1 = loadStrings('glidDriveList.txt');

}

function setup() {
  createCanvas(windowWidth, windowHeight);
  colorMode(HSB, 360, 100, 100, 100);
  frameRate(20);
  num = height*.3;
  console.log(list1);
  let day = random(131);
  heartRate = loadJSON(list1[day]);
  //heartRate = loadJSON('data/heart_rate-2020-05-01.json')

```

The sketch is working correctly when I directly indicate what file to load (commented out), but not when I try to get a name from the string array. File names also appear to be correctly loaded into array when viewed in console.

---

<div class="post-metadata">

### Author: ![fleshcircuit](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@fleshcircuit](https://discourse.processing.org/u/fleshcircuit)
#### Post date: [July 14, 2021, 10:11am UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/4 "2021-07-14T10:11:35Z")

</div>

Figured it out! I just needed to put int around the random number:

```auto
let day = int(random(0,131));
heartRate = loadJSON(list1[day]);

```

---

<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: [July 14, 2021, 12:24pm UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/5 "2021-07-14T12:24:25Z")

</div>

> [@fleshcircuit](#):
>
> `glidDriveList.txt`

we don’t know the content of this

does it look like this:

```auto
'data/heart_rate-2020-05-01.json'

```

or is **data/** missing or even **.json** mssing?

---

<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: [July 14, 2021, 12:26pm UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/6 "2021-07-14T12:26:02Z")

</div>

> [@fleshcircuit](#):
>
> `(list1[day]);`

does println (list1[day]); work?

Do you have to wait until preload() is finished or does this happen automatically?

---

<div class="post-metadata">

### Author: ![fleshcircuit](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@fleshcircuit](https://discourse.processing.org/u/fleshcircuit)
#### Post date: [July 14, 2021, 12:39pm UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/7 "2021-07-14T12:39:19Z")

</div>

Here are samples of the file names in glidDriveList.txt:

```auto
data/heart_rate-2020-05-01.json
data/heart_rate-2020-05-02.json
data/heart_rate-2020-05-03.json
data/heart_rate-2020-05-04.json

```

---

<div class="post-metadata">

### Author: ![fleshcircuit](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@fleshcircuit](https://discourse.processing.org/u/fleshcircuit)
#### Post date: [July 14, 2021, 12:39pm UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/8 "2021-07-14T12:39:39Z")

</div>

Yes, the println works.

---

<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: [July 14, 2021, 12:42pm UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/9 "2021-07-14T12:42:57Z")

</div>

> [@fleshcircuit](#):
>
> `heartRate = loadJSON(list1[day]);`

did you try `heartRate = loadJSON(list1[day].trim());`

---

<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: [July 14, 2021, 12:46pm UTC](https://discourse.processing.org/t/randomly-select-file-to-preload-p5-js/31148/10 "2021-07-14T12:46:31Z")

</div>

ah, you got it! great!
