Randomly select file to preload (p5.js)

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.

when you know the names in forehand just make a list

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

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
//

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.

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.

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

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

we don’t know the content of this

does it look like this:

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

or is data/ missing or even .json mssing?

does println (list1[day]); work?

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

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

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
1 Like

Yes, the println works.

1 Like

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

ah, you got it! great!