Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap

Q453 How can I play a random midi file when the page opens?

You are here: irt.org | FAQ | JavaScript | Sound | Q453 [ previous next ]

Output the HTML using JavaScript. If the HTML looks like:


And if you've got midi files sound1.mid throught to sound9.mid, then all you need to do is create a random number between 1 and 9, and then combine that with the rest of the file name:

// The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu)
// See:  http://www.msc.cornell.edu/~houle/JavaScript/randomizer.html

rnd.today=new Date();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
         return rnd.seed/(233280.0);

function rand(number) {
        return Math.ceil(rnd()*number);

// end central randomizer. -->


<SCRIPT LANGUAGE="JavaScript"><!--
var x = rand(9);

document.write('<EMBED SRC="sound' + x + '.mid" HIDDEN="TRUE" AUTOSTART="TRUE" LOOP="FALSE">');


©2016 Martin Webb