PDA

View Full Version : Random number script apparently not very random


Tyler Is Random
11-12-2003, 10:37 PM
I'm attempting to make a random WMA file stream on my personal blog (http://www.tylersticka.com/blog) with accompanying artist and song title beneath it. Unfortunately, upon loading it always seems to pick the number "1," as it always loads the second option here. Any help you could give would be very much appreciated, the code is below. Thank you!

var randomnumber=Math.round(Math.random()*5);
if (randomnumber=0) {
document.write('<object id="MediaPlayer" width=240 Height=45'
+ 'classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95F"'
+ 'standby="Loading Media Player"'
+ 'Type="application/x-oleobject">'
+ ''
+ ' <param name="Filename" value="file.wax">'
+ ' <embed type="application/x-mplayer2"'
+ ' src="http://www.tylersticka.com/blog/music/blur_mellowjam.wax"'
+ ' name="MediaPlayer"'
+ ' width=200'
+ ' height=45>'
+ ' </embed>'
+ '</object><br />'
+ '<center>Blur - Mellow Jam</center>');
};
else if (randomnumber=1) {
document.write('<object id="MediaPlayer" width=240 Height=45'
+ 'classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95F"'
+ 'standby="Loading Media Player"'
+ 'Type="application/x-oleobject">'
+ ''
+ ' <param name="Filename" value="file.wax">'
+ ' <embed type="application/x-mplayer2"'
+ ' src="http://www.tylersticka.com/blog/music/everclear_distorto.wax"'
+ ' name="MediaPlayer"'
+ ' width=200'
+ ' height=45>'
+ ' </embed>'
+ '</object><br />'
+ '<center>Everclear - El Distorto De Melodica</center>');
};
else if (randomnumber=2) {
document.write('<object id="MediaPlayer" width=240 Height=45'
+ 'classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95F"'
+ 'standby="Loading Media Player"'
+ 'Type="application/x-oleobject">'
+ ''
+ ' <param name="Filename" value="file.wax">'
+ ' <embed type="application/x-mplayer2"'
+ ' src="http://www.tylersticka.com/blog/music/coxon_lake.wax"'
+ ' name="MediaPlayer"'
+ ' width=200'
+ ' height=45>'
+ ' </embed>'
+ '</object><br />'
+ '<center>Graham Coxon - Lake</center>');
};
else if (randomnumber=3) {
document.write('<object id="MediaPlayer" width=240 Height=45'
+ 'classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95F"'
+ 'standby="Loading Media Player"'
+ 'Type="application/x-oleobject">'
+ ''
+ ' <param name="Filename" value="file.wax">'
+ ' <embed type="application/x-mplayer2"'
+ ' src="http://www.tylersticka.com/blog/music/gorillaz_lhsm.wax"'
+ ' name="MediaPlayer"'
+ ' width=200'
+ ' height=45>'
+ ' </embed>'
+ '</object><br />'
+ '<center>Gorillaz - Left Hand Suzuki Method</center>');
};
else if (randomnumber=4) {
document.write('<object id="MediaPlayer" width=240 Height=45'
+ 'classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95F"'
+ 'standby="Loading Media Player"'
+ 'Type="application/x-oleobject">'
+ ''
+ ' <param name="Filename" value="file.wax">'
+ ' <embed type="application/x-mplayer2"'
+ ' src="http://www.tylersticka.com/blog/music/blur_frenchsong.wax"'
+ ' name="MediaPlayer"'
+ ' width=200'
+ ' height=45>'
+ ' </embed>'
+ '</object><br />'
+ '<center>Blur - French Song</center>');
};

JAVAEOC
11-12-2003, 10:48 PM
sorry i dont know the answer to ur problem but i do know that u have a kick *** page good job :thumbsup:

kansel
11-12-2003, 10:55 PM
The problem is with your if() statements. You're using a single = which is an assignment operator. You should be using == which is a comparison operator. Also, you lack an option for when randomnumber == 5 (which will happen when using Math.round()).

if(randomnumber=0) // this evaulates to "if set randomnumber to 0" - the result of which is 0 and interpreted to false and so will never happen //

if(randomnumber=1) // this evaluates to "if set randomnumber to 1" - the result of which is 1 and interpreted to true and will always happen - and thus no other options will ever happen //

if(randomnumber == 0) // this evauates to "if randomnumber is equal to 0 //

if(randomnumber == 1) // "if randomnumber is equal to 1 //
and so on.

Since you have discrete values for randomnumber it might be better to use a switch() structure instead of nested if()s.var randomnumber=Math.floor(Math.random()*5);
// Math.floor() truncates the decimal portion giving a range of 0-4 inclusive instead of 0-5 inclusive //

switch(randomnumber){
case 0: // same as if(randomnumber == 0) //
// put statements here for if randomnumber == 0 //
break; // use break; at the end of each condition to break out of the switch statement //
case 1: // same as if(randomnumber == 1) //
// put statements here for if randomnumber == 1 //
break;
case 2:
//
break;
case 3:
//
break;
case 4:
//
break;
default:
// put failsafe code here - will randomnumber ever be anything other than 0, 1, 2, 3 or 4?
break;
}

Tyler Is Random
11-12-2003, 11:01 PM
Thanks! I'll try this out, thank you very much for your time!

Tyler Is Random
11-12-2003, 11:17 PM
It worked! You rock, Kansel.

mtm
11-13-2003, 02:26 PM
You rock too, Tyler~
Your site is artistic and entertaining!!

I enjoyed the discussion on random numbers also!

mtm:cool:

Oakendin
11-13-2003, 07:16 PM
Here's some code that lets you use arrays to hold data and not have to print out the HTML multiple times:


<script type="text/javascript">
waxfile = new Array();
waxfile[0]="http://www.tylersticka.com/blog/music/blur_mellowjam.wax";
waxfile[1]="http://www.tylersticka.com/blog/music/everclear_distorto.wax";

artistinfo = new Array();
artistinfo[0]="Blur - Mellow Jam";
artistinfo[1]="Everclear - El Distorto De Melodica";

var randomNumber = Math.floor(Math.random()*waxfile.length);
document.write('<object id="MediaPlayer" width=240 Height=45'
+ 'classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95F"'
+ 'standby="Loading Media Player"'
+ 'Type="application/x-oleobject">'
+ ''
+ ' <param name="Filename" value="file.wax">'
+ ' <embed type="application/x-mplayer2"'
+ ' src="' + waxfile[randomNumber] + '"'
+ ' name="MediaPlayer"'
+ ' width=200'
+ ' height=45>'
+ ' </embed>'
+ '</object><br />'
+ '<center>' + artistinfo[randomNumber] + '</center>');
</script>


Sorry, forgot to mention that if you need to add more, just expand the array this way:

waxfile[2] = "3rd file";
waxfile[3] = "4th file";