0

Possible Duplicate:
Randomizing elements in an array?

I found this "Quote Rotator" script on the web and it works fine for what it does, but I'd like to randomize the order the quotes are displayed instead on just cycling through the list. Any help would be appreciated.

var myquotes = new Array(
    'Quote #1',
    'Quote #2',
    'Quote #3' // Leave the last quote without a comma at the end
    );

function rotatequote()
{
    thequote = myquotes.shift(); //Pull the top one
    myquotes.push(thequote); //And add it back to the end

    document.getElementById('quotetext').innerHTML = thequote;
    t=setTimeout("rotatequote()",10000);
}

// Start the first rotation.
rotatequote();
Community
  • 1
  • 1

1 Answers1

1

Change rotateQuote to:

function rotatequote()
{
    var thequote = myquotes[( Math.floor ( Math.random() * myquotes.length ) )];
    document.getElementById('quotetext').innerHTML = thequote;
    var t=setTimeout("rotatequote()",10000);
}
Lusitanian
  • 11,012
  • 1
  • 41
  • 38
  • this could show the same quote twice in a row -- it sounds like he would prefer a shuffle – mpen Jul 08 '12 at 00:29
  • Ah, I misinterpreted what he wanted. – Lusitanian Jul 08 '12 at 00:30
  • He does say that he wants to randomize the order that the quotes are displayed. When you randomize something you get the chance that the same item can get selected multiple times in a row. You could shuffle the array each time, but then again, if its truly a random shuffle the same item could get shuffled to the front of the array. – HeatfanJohn Jul 08 '12 at 00:37
  • thanks David... but just replacing the rotatequote bit you gave stops the script from working. – user1509413 Jul 08 '12 at 18:54