Welcome to WebmasterWorld Guest from 23.20.162.200

Forum Moderators: open

Message Too Old, No Replies

# how do i choose a random function?

## need to select two, out of a possible eight

#### londrum

8:18 pm on Aug 29, 2007 (gmt 0)

#### Senior Member

joined:Feb 12, 2006
posts:2650

hi... hoping you can help me with a little problem i've got.

i've got a little script which writes some HTML onto the page, together with an image and a link. and i've wrapped it up inside a function.

i have eight of these functions, all writing something different to the page.

they are called

``function random1(){ blah blah blah } function random2(){ blah blah blah } function random3(){ blah blah blah }``
etc... up to
``function random8``

my problem is, i need to find a way of choosing just two of them (it's got to be two different ones each time).

i know how you can generate a random number between 1 and 8, but how can you generate TWO random numbers between 1 and 8, making sure that each one is different?

it's driving me nuts

#### Trace

8:34 pm on Aug 29, 2007 (gmt 0)

#### Preferred Member

joined:July 5, 2005
posts:352

This will produce 2 different random numbers;
<script type="text/javascript">
var randomNum1=Math.floor(Math.random()*8);
var randomNum2=randomNum1;
while (randomNum2 == randomNum1){
randomNum2=Math.floor(Math.random()*8);
}

alert(randomNum1 + ' and ' + randomNum2);
</script>

#### londrum

8:51 pm on Aug 29, 2007 (gmt 0)

#### Senior Member

joined:Feb 12, 2006
posts:2650

thankyou my friend, that chooses the two numbers perfect. but how do i then take those variable names (randomNum1, and randomNum1) and include them as part of the function name?

so i need to get

``function random1 () { }``

from

``function random[randomNum1] () { }``

...this is also driving me nuts

#### Gibble

8:56 pm on Aug 29, 2007 (gmt 0)

#### Senior Member

joined:June 13, 2002
posts:662

``for (i = 1; i<=2; i++) { var n = Math.floor(Math.random()*8); switch(n) { case 1: random1(); break  case 2: random2(); break ...`` case 8: random8(); }}``

And you can throw in a while statement to see if the new random variable is the same as the last one grabbed, and keep getting random numbers until they are different like Trace did in his(?) code.

[edited by: Gibble at 8:59 pm (utc) on Aug. 29, 2007]

#### Little_G

9:05 pm on Aug 29, 2007 (gmt 0)

#### Preferred Member

joined:Feb 11, 2006
posts:481

Hi,

You can use the eval function:

``eval("random" + Math.ceil(Math.random()*8) + "()");``

Andrew

#### londrum

9:34 pm on Aug 29, 2007 (gmt 0)

#### Senior Member

joined:Feb 12, 2006
posts:2650

i like that, that comes up with what i want, but how do i get it to actually call the function.
i've tried writing:

``function (eval("random" + Math.ceil(Math.random()*8) + "();"));``

i need it looking like:

``function random5();``

if i stick the eval function above in an alert box, it does come up with

``random5();``

#### Little_G

9:41 pm on Aug 29, 2007 (gmt 0)

#### Preferred Member

joined:Feb 11, 2006
posts:481

Hi,

It works like this:

``function random1(){return 1;}function random2(){return 2;}function random3(){return 3;}``
``alert(eval("random" + Math.ceil(Math.random()*3) + "()"));``

Andrew

[edited by: Little_G at 9:42 pm (utc) on Aug. 29, 2007]

#### londrum

9:42 pm on Aug 29, 2007 (gmt 0)

#### Senior Member

joined:Feb 12, 2006
posts:2650

don't worry, it was me being dopey.
``eval("random" + Math.ceil(Math.random()*8) + "()");``

on its own, like you said! works perfect.
that was just me assuming it was more difficult than it actually was.

#### Steerpike

6:00 am on Aug 30, 2007 (gmt 0)

#### Junior Member

joined:Oct 27, 2005
posts:112

Out of personal curiosity why are you creating a bunch of random functions rather than creating one function and doing the randomising within that?

Is there a particular reason why you chose the method you did?

#### londrum

9:31 am on Aug 30, 2007 (gmt 0)

#### Senior Member

joined:Feb 12, 2006
posts:2650

i'm trying to write stuff to the page the long-winded way - without using document.write. (my pages are actually served as XML, so i can't use document.write.)
that means i have to create all the elements, write in all the text, links and images. and i've got eight different versions of everything. it just seemed easier to put them each in a different function. but i'm a newbie with all this javascript stuff (as you can probably tell) so if there's a better way... be happy to do it.

i've got it working now, but i've come up against a javascript error in IE.
it doesn't really matter, though, because the script still fundtions perfectly well. but it keeps throwing up an 'Not implemented' error. Anyone know how to get rid of that?

Here is my (long-winded) script. I have cut some of it out to keep it short.

``function pics(){``function random1(){var pic1=document.createElement('div');pic1.setAttribute('class','pic');pic1.setAttribute('className','pic');pic1.setAttribute('style','background:url(/images/example.gif) no-repeat 0% 0%');pic1.style.cssText="background:url(/images/example.gif) no-repeat 0% 0%;";var span1=document.createElement('span');var pic1=document.createElement('a');span1.appendChild(pic1);pic1.setAttribute('href','http://www.example.com');pic1.appendChild(document.createTextNode('Anchor text'));span1.appendChild(document.createTextNode('Some description text'));pic1.appendChild(span1);var scr=document.getElementById('pics');scr.parentNode.insertBefore(pic1,scr);}function random2(){var pic2=document.createElement('div');pic2.setAttribute('class','pic');pic2.setAttribute('className','pic');pic2.setAttribute('style','background:url(/images/example.gif) no-repeat 0% 0%');pic2.style.cssText="background:url(/images/example.gif) no-repeat 0% 0%;";var span2=document.createElement('span');var pic2=document.createElement('a');span2.appendChild(pic2);pic1.setAttribute('href','http://www.example.com');pic2.appendChild(document.createTextNode('Anchor text'));span2.appendChild(document.createTextNode('Some description text'));pic2.appendChild(span2);var scr=document.getElementById('pics');scr.parentNode.insertBefore(pic2,scr);}``

.... etc... up to random4

``addLoadListener(eval("random" + Math.ceil(Math.random()*4)));}``

i know that the addLoadListener function works fine, so it's nothing to do with that.

#### Little_G

10:30 am on Aug 30, 2007 (gmt 0)

#### Preferred Member

joined:Feb 11, 2006
posts:481

Hi,

You might be better of with something like this:

``function create(r){switch(r){case 1:var pic1=document.createElement('div');pic1.setAttribute('class','pic');pic1.setAttribute('className','pic');pic1.setAttribute('style','background:url(/images/example.gif) no-repeat 0% 0%');pic1.style.cssText="background:url(/images/example.gif) no-repeat 0% 0%;";var span1=document.createElement('span');var pic1=document.createElement('a');span1.appendChild(pic1);pic1.setAttribute('href','http://www.example.com');pic1.appendChild(document.createTextNode('Anchor text'));span1.appendChild(document.createTextNode('Some description text'));pic1.appendChild(span1);var scr=document.getElementById('pics');scr.parentNode.insertBefore(pic1,scr);break;``
``case 2:var pic2=document.createElement('div');pic2.setAttribute('class','pic');pic2.setAttribute('className','pic');pic2.setAttribute('style','background:url(/images/example.gif) no-repeat 0% 0%');pic2.style.cssText="background:url(/images/example.gif) no-repeat 0% 0%;";var span2=document.createElement('span');var pic2=document.createElement('a');span2.appendChild(pic2);pic1.setAttribute('href','http://www.example.com');pic2.appendChild(document.createTextNode('Anchor text'));span2.appendChild(document.createTextNode('Some description text'));pic2.appendChild(span2);var scr=document.getElementById('pics');scr.parentNode.insertBefore(pic2,scr);break;.... etc... up to random4}``
``addLoadListener(create(Math.ceil(Math.random()*4)));``

It avoids the use of eval which is normally quite slow.

Andrew

#### londrum

11:26 am on Aug 30, 2007 (gmt 0)

#### Senior Member

joined:Feb 12, 2006
posts:2650

yup i'll change it to that.
i'm still getting that annoying little javascript error with IE though.
it doesn't stop the script from working though.

i've narrowed it down to these two lines (this is just example code, not the actual code):

``var pic=document.createElement('img');pic.setAttribute('href','http://www.example.com');``

for some reason, IE7 doesn't like it when you set an href attribute on an <img> tag. when i remove the line, the 'Not implemented' error disappears, so it must be that.

Strangely, it doesn't mind if you set an href attribute on an <a> tag. what gives?

#### rsndll

11:49 am on Aug 30, 2007 (gmt 0)

#### New User

joined:Mar 22, 2006
posts:38

HREF isn't an attribute of the IMG element so that would be why IE7 doesn't like it.

#### londrum

12:05 pm on Aug 30, 2007 (gmt 0)

#### Senior Member

joined:Feb 12, 2006
posts:2650

duh, of course. i should have put
``src``
.
thanks for all your help again, i'm sorted now.