Please, how to add a random function to this?

A

Amy

Hello, I need to make this select an array item randomly instead of in
order. Anyone
know how? Thank you very much. Amy

<BODY onload="changelink();"><a href="#" id="url"></a>

var arr = [
["Text"],["http://www.google.com"],
["Text"],["http://www.yahoo.com"]
];

var dynLink = null;

function init() {
dynLink = document.getElementById('url');
window.setTimeout('changeURL(0)',1000);
}

function changeURL(i) {
if (i >= arr.length) {
i = 0;
}
dynLink.innerHTML = arr[0];
dynLink.href = arr[1];
window.setTimeout('changeURL('+(++i)+')',1000);
}
window.onload = init;
 
M

marss

Amy напиÑав:
function changeURL(i) {
if (i >= arr.length) {
i = 0;
}
dynLink.innerHTML = arr[0];
dynLink.href = arr[1];
window.setTimeout('changeURL('+(++i)+')',1000);
}


change to:
i = Math.round(Math.random()* arr.length)
 
M

Michael Winter

marss wrote:

[snip]
i = Math.round(Math.random()* arr.length)

Rounding creates the wrong range. Consider:

Math.round(Math.random() * 2);

The intended set of possible results is {0,1}. However, 2x >= 1.5 when x
= 0.75. The value of x (the result of the random method) will exceed
0.75, and 1.5 or greater rounds up to 2.

Moreover, rounding creates an uneven distribution where the first and
last numbers will occur with half the frequency of the others.

Use the floor method:

Math.floor(Math.random() * arr.length);

Mike
 
A

Amy

Gosh thanks but I have no idea how to put it in the function instead.
Can someone help me?


Michael said:
marss wrote:

[snip]
i = Math.round(Math.random()* arr.length)

Rounding creates the wrong range. Consider:

Math.round(Math.random() * 2);

The intended set of possible results is {0,1}. However, 2x >= 1.5 when x
= 0.75. The value of x (the result of the random method) will exceed
0.75, and 1.5 or greater rounds up to 2.

Moreover, rounding creates an uneven distribution where the first and
last numbers will occur with half the frequency of the others.

Use the floor method:

Math.floor(Math.random() * arr.length);

Mike
 
A

Amy

I tried this, doesn't seem to work.


function changeURL(i) {
if (i >= arr.length) {
i = Math.floor(Math.random() * arr.length);
}
dynLink.innerHTML = arr[0];
dynLink.href = arr[1];
window.setTimeout('changeURL('+(++i)+')',1000);

}


window.onload = init;


Michael said:
marss wrote:

[snip]
i = Math.round(Math.random()* arr.length)

Rounding creates the wrong range. Consider:

Math.round(Math.random() * 2);

The intended set of possible results is {0,1}. However, 2x >= 1.5 when x
= 0.75. The value of x (the result of the random method) will exceed
0.75, and 1.5 or greater rounds up to 2.

Moreover, rounding creates an uneven distribution where the first and
last numbers will occur with half the frequency of the others.

Use the floor method:

Math.floor(Math.random() * arr.length);

Mike
 
A

Amy

OK its working, thank you very much. One thing though it always starts
with the very first one in the array. Any idea why? If it can't be
fixed just let me know.
 
A

Amy

Oh man, I figured out whats wrong, my items in the array aren't working
correctly. Have to start a new thread.
 
M

Michael Winter

Amy said:
I tried this, doesn't seem to work.

function changeURL(i) {
if (i >= arr.length) {
i = Math.floor(Math.random() * arr.length);
}
dynLink.innerHTML = arr[0];
dynLink.href = arr[1];
window.setTimeout('changeURL('+(++i)+')',1000);

}


The argument, i, is now redundant: the index is always obtained by
random selection (which also means removing the condition).

function changeUrl() {
var i = Math.floor(Math.random() * arr.length);

dynLink.innerHTML = arr[0];
dynLink.href = arr[1];

setTimeout('changeUrl()', 1000);
}

A second (or thereabouts) is rather too frequent, isn't it?


From your original post:

var arr = [
["Text"],["http://www.google.com"],
["Text"],["http://www.yahoo.com"]
];

Is that copied code, or a result of transcribing? Either way, it's wrong:

var arr = [['Google', 'http://www.google.com/'],
['Yahoo!', 'http://www.yahoo.com/']];

The former would create an array with four elements, each of which
containing an array with one element each. The latter would create an
array with two elements, each of which containing an array with two
elements each.

[snip]

Mike


Please don't top-post when replying to this group.
 
M

Michael Winter

Amy said:
Thanks, what does top posting mean?

This group's FAQ contains detailed information on posting, including a
description of top-posting.

function changeUrl() {
var i = Math.floor(Math.random() * arr.length);

dynLink.innerHTML = arr[0];
dynLink.href = arr[1];

setTimeout('changeUrl()', 1000);
}


Tried this, it isn't working.


The function above is fine. Your usage of it, perhaps, is not. Post a
link to a demonstration and I can tell you what you did wrong.

Mike
 
A

Amy

Thanks, I took the array ASM ajusted and added yours to it<body
onload="changeURL()"><a href="#" id="url"></a>

var arr = [
["Link name google web 1","http://www.google.com"],
["Link name yahoo web site 2","http://www.yahoo.com"],
["Link name google web site 3","http://www.google.com"]
];

function changeURL() {
var i = Math.floor(Math.random() * arr.length);
dynLink.innerHTML = arr[0];
dynLink.href = arr[1];
setTimeout('changeURL()', 1000); }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top