Js random actions

 
<SCRIPT language="Javascript">
<!--
rnd.today=new Date();
rnd.seed=rnd.today.getTime();

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>
<SCRIPT language="Javascript">
<!--
picnum = rand(4);
    if (picnum==1) {img="http://www.morsedesigns.com//bigk/1.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==2) {img="http://www.morsedesigns.com//bigk/2.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==3) {img="http://www.morsedesigns.com//bigk/3.gif"; url="http://www.morsedesigns.com"; }
else                {img="http://www.morsedesigns.com//bigk/4.gif"; url="http://www.morsedesigns.com"; }  

document.write('<a href="'+url+'"><img src="'+img+'"></a>');

// -->
</SCRIPT>

help ^ no work

Curious why you don’t see what’s in the tag… Hm… Strange…

*Originally posted by morse *
**

<SCRIPT language="Javascript">
<!--
rnd.today=new Date();
rnd.seed=rnd.today.getTime();

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>
<SCRIPT language="Javascript">
<!--
picnum = rand(4);
    if (picnum==1) {img="http://www.morsedesigns.com//bigk/1.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==2) {img="http://www.morsedesigns.com//bigk/2.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==3) {img="http://www.morsedesigns.com//bigk/3.gif"; url="http://www.morsedesigns.com"; }
else                {img="http://www.morsedesigns.com//bigk/4.gif"; url="http://www.morsedesigns.com"; }  

document.write('<a href="'+url+'"><img src="'+img+'"></a>');

// -->
</SCRIPT>

<SCRIPT language="Javascript">
<!--
rnd.today=new Date();
rnd.seed=rnd.today.getTime();

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>
<SCRIPT language="Javascript">
<!--
picnum = rand(4);
    if (picnum==1) {img="http://www.morsedesigns.com//bigk/1.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==2) {img="http://www.morsedesigns.com//bigk/2.gif"; url="http://www.morsedesigns.com"; }
else if (picnum==3) {img="http://www.morsedesigns.com//bigk/3.gif"; url="http://www.morsedesigns.com"; }
else                {img="http://www.morsedesigns.com//bigk/4.gif"; url="http://www.morsedesigns.com"; }  

document.write('<a href="'+url+'"><img src="'+img+'"></a>');

// -->
</SCRIPT>

**

eh? when i posted with code brackets it didnt work, but i changed to php if that is what you mean.

Why don’t you store the images in array and choose randomly from that array?

Like this? Something is wrong… I suck too much to figure out what…


<SCRIPT LANGUAGE="JAVASCRIPT">
var img_rnd = new Array ("1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif", "7.gif", "8.gif", "9.gif", "10.gif", "11.gif", "12.gif");
var i = Math.round(Math.random() * 11);
document.write('<a href="http://www.morsedesigns.com" target="_new"> <IMG SRC="www.morsedesigns.com//bigk/' + img_rnd*>');
</SCRIPT>

What about this??? You had non matching quotes problem… I do see the random file names with the alert() after I fixed that problem…

<SCRIPT LANGUAGE="JAVASCRIPT">
	var img_rnd = new Array ("1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif", "7.gif", "8.gif", "9.gif", "10.gif", "11.gif", "12.gif");
	var i = Math.round(Math.random() * 11);
	alert(img_rnd*);
	document.write('<a href="http://www.morsedesigns.com" target="_new"> <IMG SRC="www.morsedesigns.com//bigk/" + img_rnd*>');
</SCRIPT>
<SCRIPT LANGUAGE="JAVASCRIPT">
    var img_rnd = new Array ("1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif", "7.gif", "8.gif", "9.gif", "10.gif", "11.gif", "12.gif");
    var i = Math.round(Math.random() * 11);
    alert(img_rnd*);
    document.write('<a href="http://www.morsedesigns.com" target="_new"> <IMG SRC="www.morsedesigns.com//bigk/" + img_rnd*>');
</SCRIPT>

[edit by lostinbeta]forgot the php tags ;)[/edit]

Still unmatching quotes…

<SCRIPT LANGUAGE="JAVASCRIPT">
    var img_rnd = new Array ("1.gif", "2.gif", "3.gif", "4.gif", "5.gif", "6.gif", "7.gif", "8.gif", "9.gif", "10.gif", "11.gif", "12.gif");
    var i = Math.round(Math.random() * 11);
    alert(img_rnd*);
    document.write("<a href='http://www.morsedesigns.com' target='_blank'> <IMG SRC='www.morsedesigns.com/bigk/'" + img_rnd*+">");
</SCRIPT>

Also you had the target as _new it should be _blank and your moredesigns.com//bigk should only be one forward slash.

Yup… My bad… :blush:

And while I am at it, I made some slight adjustments…

<SCRIPT LANGUAGE="JAVASCRIPT">
    var img_rnd = 
	["1.gif",
	"2.gif", 
	"3.gif", 
	"4.gif", 
	"5.gif", 
	"6.gif", 
	"7.gif", 
	"8.gif", 
	"9.gif", 
	"10.gif", 
	"11.gif", 
	"12.gif"]
    var i = Math.floor(Math.random()*img_rnd.length);
    alert(img_rnd*);
    document.write("<a href='http://www.morsedesigns.com' target='_blank'> <IMG SRC='http://www.morsedesigns.com/bigk/'" + img_rnd* + ">");
</SCRIPT>

I lined the array up like that just because it makes it easier to read and edit then when it is one really long line.
Then I changed the i variable to be a more efficient equation that uses Math.floor bceause arrays start counting at 0, and with Math.round() it is very rare if at all you will get 1.gif in that case. I also multiplied the random number by the length of the img_rnd array so that you won’t have to update this equation when you update the array.

I didn’t test it, but it should work.

Wait Wait… I got a chance to test it, that script doesn’t work, I got the quotation wrong (I knew I shoulda tested it!)

<SCRIPT LANGUAGE="JAVASCRIPT">
    var img_rnd = 
    ["1.gif",
    "2.gif", 
    "3.gif", 
    "4.gif", 
    "5.gif", 
    "6.gif", 
    "7.gif", 
    "8.gif", 
    "9.gif", 
    "10.gif", 
    "11.gif", 
    "12.gif"]
    var i = Math.floor(Math.random()*img_rnd.length);
	var addImage = img_rnd*;
	alert(addImage);
    document.write("<a href='http://www.morsedesigns.com' target='_blank'> <IMG SRC=http://www.morsedesigns.com/bigk/" + addImage + "></A>");
</SCRIPT>

Works great.

I added a variable for img_rnd* because you use it twice, so for easier updating if ever necessary you can just edit that. I also forgot to close the A tag.

Wow Shane thanks man =) Your good at this. Yes, you are. Thanks again man. I know nothing about JS.

And CyanBlue, thanks also!

:slight_smile: :slight_smile: :slight_smile:

No problem man… JS was my thing before AS as you know :slight_smile: