How to shuffle an array of 50 values, but from 1.000 possible values?

Hi!!

I'm using this algorithm to shuffle an array of more than a 1000 int values, to create a random list of files to be played in an MP3 module. Each file number is a song, so I'd get everytime the module is powered a random list of files to be played.

Unfortunately my Attiny167 has little RAM, so the max array I can now use is 50 int values.

So is there a way to adapt this code to be able to get the max number of files in the sd card, let's say, 2500 files, and create an array with 50 random numbers from these 2.500?

When these 50 random songs were played, I can make it launch this function again and have 50 more random songs from the total amount.

Can it be done? Thanks!

void createRandomList() {

  for (int v = 0; v < file_Count; v++) {
    random_List[v] = v + 1;
  }

  for (int g = (file_Count - 1); g > 0; --g) {
    int p = random(0, g + 1); // 0 ≤ p ≤ g
 
    int t = random_List[g];
    random_List[g] = random_List[p];
    random_List[p] = t;
  }

}

What I would do:

  • Start with a list of any 50 random numbers and an index variable set to 0.
  • When time comes to play a new file, pick random numbers until you find one that is not in the list. That becomes the next song to play.
  • Put that number into the list at the index. Increment the index. If the index reaches 50, set it to 0.
  • Play the chosen song. When it ends, go to Step 2.

This will give you songs in random order but no song will repeat within 50 songs.

Generate a random number
2)
Check if file exists
3)
If not back to (1)
4)
If in array already, back to (1)
5)
Add to array
5)
If not 50 numbers, back to (1)
6)
Start playing.