0

I have a function randomPlay associated to event onended but the random number is always the same on Firefox. On Chrome it's okay. Anyone can explains why and how to fix?

 function randomPlay(){
    $("audio").html('<source src="'+srcArray[Math.floor(Math.random() * srcArray.length)]+'" type="audio/mpeg">');
    document.getElementById('audio').play();
    }

The element is this audio:

<audio id="audio" autoplay onended="randomPlay();" ><source src="music.mp3" type="audio/mpeg"></audio>

UPDATE

I changed the element to:

<audio id="audio" autoplay onended="randonPlay();" src=""></audio>

and changed the function to:

function randonPlay(){
    $("audio").attr("src",srcArray[Math.floor(Math.random() * srcArray.length)]);
    document.getElementById('audio').play();
}

So all is fine now :) Thank You for your help all. Anyway I also found a lib to generate random numbers Chance.js

germanosk
  • 15
  • 3
  • See this post: http://stackoverflow.com/questions/1972550/math-random-not-random – Damo Nov 18 '13 at 02:22
  • That really is strange, even MDN (the most authoritative source for MDN) says it should be seeded by time. – Amadan Nov 18 '13 at 02:23

1 Answers1

0

It's because the generator is seeded when the page loads. See this post for some info:

Here: Math.random() - Not random

Community
  • 1
  • 1
Damo
  • 361
  • 4
  • 16
  • Thank You so much. I debug after and found the Random isn't the real problem. Anyway I found this awesome lib to generate Random numbers [link](http://chancejs.com/) . – germanosk Nov 18 '13 at 03:57