Creating a Char Array with random number

Hi all, I'm a bit stumped as to how to fix this. The goal is to play a random file from a selected folder using my Arduino Nano and a Catalex MP3 Player. The MP3 player must receive a command formatted like this: 0x0301 where "0x0" is the prefix and "301" is the folder and file number (in this example, it would read from folder 3, file 01. To do this, I've taken the example code from cefaloid and a suggestion from Grumpy_Mike to concatenate the two strings. Problem I'm currently facing is that I'm getting extra wierd symbols after the strcat is printed. I found this post, which tells me I'm doing something wrong in generating the number for the char array, I just don't understand how to implement the code there or how to fix my own code to correct this.

char toPlay[8];     //  string array for file to play 00.WAV to 99999.WAV
long randNumber;
long indexToWrite = 0;
char audioAppend[] = "0x0";


//When the robot detects motion, he will wake up
int inputPin = 7;  //input pin for PIR sensor
int pirState = LOW; //start by assuming no motion is detected
int val = 0; //variable for reading the pin status


void setup() {
  // put your setup code here, to run once:
pinMode(inputPin, INPUT);  //declare sensor as input
  Serial.begin(9600);

}

void loop() {

randNumber = random(300,305);

  // put your main code here, to run repeatedly:
val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {  
     if (pirState == LOW) {
      // we have just turned on
      makeName(randNumber,0);
      Serial.println(toPlay);
      Serial.println(audioAppend);
      // then pass this function a random in the range of the files you have
      // We only want to print on the output change, not state
      pirState = HIGH;

    }
  } else {
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
  

}


void makeName(int number, int depth){  // generates a file name 0.WAV to 9999.WAV suppressing leading zeros
  if(number > 9) {
    makeName(number / 10, ++depth); // recursion
    depth--;
    number = number % 10;   // only have to deal with the next significant digit of the number
     }
  toPlay[indexToWrite] = (number & 0xf) | 0x30;
  indexToWrite++;
  if(depth > 0) return; // return if we have more levels of recursion to go
  else {  // finish off the string with the wave extesion
strcat(audioAppend,toPlay);
    // toPlay[indexToWrite] = '.';
     //toPlay[1+indexToWrite] = 'W';
    // toPlay[2+indexToWrite] = 'A';
    // toPlay[3+indexToWrite] = 'V';
   //  toPlay[4+indexToWrite] = '\0'; // terminator
     indexToWrite = 0; // reset pointer for next time we enter 
}
}

Grumpy_Mike's Random Song player code HERE

Documentation for the Catalex MP3 player I'm using HERE

Sample code from cefaloid HERE

Made some progress today after I came across this post on using the sprintf function: https://forum.arduino.cc/index.php?topic=46884.0. I simplified things using this one line of code to add the “0x0” prefix to an int called randNumber. I’m not sure why it didn’t work the other way, maybe you guys can tell me?

sprintf(toPlay, "0x0%i", randNumber);

I also wanted to make sure I didn’t have any repeating numbers so I came across this article:

for (size_t i = 0; i < 36; i++) {
  size_t j = random(i, 36);

  // swap x[i] and x[j]
  auto t = x[i];
  x[i] = x[j];
  x[j] = t;
  randNumber = t;

The code above fills an array with numbers, then shuffles them somehow to produce more random numbers. This is called the Fisher-Yates model, the first half of this YouTube video does a good job at explaining how it works: Fisher-Yates Shuffle Modern Algorithm JavaScript Programming Tutorial - YouTube

Anyway, my full working code is:

int randNumber ;
char toPlay[8];     //  string array for file to play 00.WAV to 99999.WAV
uint8_t x[36];

//When the robot detects motion, he will wake up
int inputPin = 7;  //input pin for PIR sensor
int pirState = LOW; //start by assuming no motion is detected
int val = 0; //variable for reading the pin status


void setup() {
  // put your setup code here, to run once:
pinMode(inputPin, INPUT);  //declare sensor as input
  Serial.begin(9600);

}

void loop() {


for (size_t i = 0; i < 36; i++) x[i] = i+1;


  // put your main code here, to run repeatedly:
val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {  
     if (pirState == LOW) {
      // we have just turned on
      for (size_t i = 0; i < 36; i++) {
  size_t j = random(i, 36);

  // swap x[i] and x[j]
  auto t = x[i];
  x[i] = x[j];
  x[j] = t;
  randNumber = t;
}
      sprintf(toPlay, "0x0%i", randNumber);
      Serial.println (toPlay);
      // then pass this function a random in the range of the files you have
      // We only want to print on the output change, not state
      pirState = HIGH;

    }
  } else {
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }

}

I'm not sure why it didn't work the other way, maybe you guys can tell me?

The "toPlay" character array was not properly zero-terminated.