[Solved] Random HTML Code generated by Javascript?


What do you mean by random source? If you generate a random string for your source it probably wouldn’t be valid. What I think you want is to have an array of valid sources and select one of those valid sources at random.

In which case you would do this:

HTML

<audio class="audio-element" controls="true" preload="none" id='audio'>

JavaScript

Math.randInt = function(min, max){
  return Math.floor((Math.random()*((max+1)-min))+min);
};
var sources = [
    "source1.mp3",
    "source2.mp3",
    "source3.mp3"
];
document.getElementById("audio").src = sources[Math.randInt(0, sources.length-1)];

This will pick a random source from an array of valid sources which I think is what you want. But if you want to generate a “random source” you can do something stupid like this:

HTML

<audio class="audio-element" controls="true" preload="none" id='audio'>

JavaScript

Math.randInt = function(min, max){
  return Math.floor((Math.random()*((max+1)-min))+min);
};
document.getElementById("audio").src = Math.randInt(999999999,9999999999).toString(36);+".mp3";

That will generate a random string of numbers and letters as your source. But why?

solved Random HTML Code generated by Javascript?