Play a random song with buzzer

I have written the following code, to play a random song of 20 length.

But it produces first 3 seconds good random song then produces same tune for rest of time.

How can i make it play random song till length specified?

const int buzzerPin = 9;

// We'll set up an array with the notes we want to play
// change these values to make different songs!

// Length must equal the total number of notes and spaces 

//const int songLength = 18;

// Notes is an array of text characters corresponding to the notes
// in your song. A space represents a rest (no tone)

//generate random song



// Beats is an array of values for each note and rest.
// A "1" represents a quarter-note, 2 a half-note, etc.
// Don't forget that the rests (spaces) need a length as well.

int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};

// The tempo is how fast to play the song.
// To make the song play faster, decrease this value.

int tempo = 150;

char song[] = {'c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' '};
const byte songLength = sizeof(song) / sizeof(song[0]);
char notes[21];  //allow 1 extra for the NULL


void setup() 
{
  pinMode(buzzerPin, OUTPUT);
  pinMode (13,OUTPUT);
  Serial.begin(9600);
  





}


void loop() 
{
  
  Serial.begin(115200);
  for (int n = 0; n < 20 ; n++)
  {
    notes[n] = song[random(0, songLength)];
    notes[n + 1] = '\0';
    Serial.println(notes);
  }
  
  
   /* char song[] = {'c', 'd',' ', 'e', 'f',' ', 'g', 'a', 'b', 'C',' '};
char fs[]={};
int lengthh=20; //specify song length

int ii;

for(ii = 0;ii <= lengthh; ii++){
  
 fs[ii] =  song[random (0, 10)];
  
}


//String str(fs);

Serial.print(fs);

//char notes[] = "cdefg cf ddeegg Cbaag "; // a space represents a rest
int ih;

char notes[]="";

  for(ih=0;ih<=lengthh;ih++){
  
  notes[ih] = fs[ih];
}
  
//Serial.write("write this");
*/

const int songLength = 20;


  int i, duration;
  
  for (i = 0; i < songLength; i++) // step through the song arrays
  {
    duration = beats[i] * tempo;  // length of note/rest in ms
    
    digitalWrite(13,HIGH);
    
    if (notes[i] == ' ')          // is this a rest? 
    {
      delay(duration);      // then pause for a moment
      digitalWrite(13,LOW);

    }
    else                          // otherwise, play the note
    {
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);            // wait for tone to finish
    }
    delay(tempo/10);              // brief pause between notes
  }
  
  // We only want to play the song once, so we'll pause forever:
  while(true){}
  // If you'd like your song to play over and over,
  // remove the above statement
        digitalWrite(13,LOW);

}


int frequency(char note) 
{
  // This function takes a note character (a-g), and returns the
  // corresponding frequency in Hz for the tone() function.
  
  int i;
  const int numNotes = 8;  // number of notes we're storing
  
  // The following arrays hold the note characters and their
  // corresponding frequencies. The last "C" note is uppercase
  // to separate it from the first lowercase "c". If you want to
  // add more notes, you'll need to use unique characters.

  // For the "char" (character) type, we put single characters
  // in single quotes.

  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  
  // Now we'll search through the letters in the array, and if
  // we find it, we'll return the frequency for that note.
  
  for (i = 0; i < numNotes; i++)  // Step through the notes
  {
    if (names[i] == note)         // Is this the one?
    {
      return(frequencies[i]);     // Yes! Return the frequency
    }
  }
  
  return(0);  // We looked through everything and didn't find it,
              // but we still need to return a value, so return 0.
}

Is this the same question as your other thread asked in a different way ?

UKHeliBob: Is this the same question as your other thread asked in a different way ?

Related to it.

it produces first 3 seconds good random song then produces same tune for rest of time

I seriously doubt that seeing as random() produces the same sequence every time the program is run.

How can i make it play random song till length specified?

Look at the randomSeed() function but you will need a way to seed it randomly.......

A search for randomSeed on the forum will return plenty of hits.

UKHeliBob: I seriously doubt that seeing as random() produces the same sequence every time the program is run. Look at the randomSeed() function but you will need a way to seed it randomly.......

A search for randomSeed on the forum will return plenty of hits.

Where I have to seed ( I mean where I have to place randomSeed() function? )

Where I have to seed ( I mean where I have to place randomSeed() function? )

Before the first time you use random(), but what are you going to use as the seed value ?

UKHeliBob: Before the first time you use random(), but what are you going to use as the seed value ?

Oh no! What should I use?

Oh no! What should I use?

Search the forum for randomSeed() for a variety of suggestions.