# Js random actions

**URL:** <https://forum.kirupa.com/t/js-random-actions/14207>\
**Category:** Uncategorized\
**Created:** [February 23, 2003, 10:44am UTC](https://forum.kirupa.com/t/js-random-actions/14207 "2003-02-23T10:44:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![morse](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@morse](https://forum.kirupa.com/u/morse)\
**Post date:** [February 23, 2003, 10:44am UTC](https://forum.kirupa.com/t/js-random-actions/14207/1 "2003-02-23T10:44:27Z")

</div>

```php
 
<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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 10:50am UTC](https://forum.kirupa.com/t/js-random-actions/14207/2 "2003-02-23T10:50:04Z")

</div>

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

> \*Originally posted by morse \*  
> \*\*

```php
<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>

```

\*\*

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 10:52am UTC](https://forum.kirupa.com/t/js-random-actions/14207/3 "2003-02-23T10:52:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 6:11pm UTC](https://forum.kirupa.com/t/js-random-actions/14207/4 "2003-02-23T18:11:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:12pm UTC](https://forum.kirupa.com/t/js-random-actions/14207/5 "2003-02-23T21:12:36Z")

</div>

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

```php

<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>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 9:49pm UTC](https://forum.kirupa.com/t/js-random-actions/14207/6 "2003-02-23T21:49:10Z")

</div>

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

```php
<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>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 10:07pm UTC](https://forum.kirupa.com/t/js-random-actions/14207/7 "2003-02-23T22:07:55Z")

</div>

```php
<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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 10:53pm UTC](https://forum.kirupa.com/t/js-random-actions/14207/8 "2003-02-23T22:53:02Z")

</div>

Still unmatching quotes…

```php
<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](http://moredesigns.com//bigk) should only be one forward slash.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 11:06pm UTC](https://forum.kirupa.com/t/js-random-actions/14207/9 "2003-02-23T23:06:00Z")

</div>

Yup… My bad… 😊

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 11:13pm UTC](https://forum.kirupa.com/t/js-random-actions/14207/10 "2003-02-23T23:13:54Z")

</div>

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

```php
<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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 23, 2003, 11:31pm UTC](https://forum.kirupa.com/t/js-random-actions/14207/11 "2003-02-23T23:31:33Z")

</div>

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!)

```php
<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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 24, 2003, 2:05am UTC](https://forum.kirupa.com/t/js-random-actions/14207/12 "2003-02-24T02:05:55Z")

</div>

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

And CyanBlue, thanks also!

🙂 🙂 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 24, 2003, 4:08am UTC](https://forum.kirupa.com/t/js-random-actions/14207/13 "2003-02-24T04:08:55Z")

</div>

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