473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Please, how to add a random function to this?

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[i][0];
dynLink.href = arr[i][1];
window.setTimeout('changeURL('+(++i)+')',1000);
}
window.onload = init;

Nov 3 '06 #1
11 1577

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

Nov 3 '06 #2
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
Nov 3 '06 #3
Amy
Gosh thanks but I have no idea how to put it in the function instead.
Can someone help me?
Michael Winter wrote:
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
Nov 3 '06 #4
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[i][0];
dynLink.href = arr[i][1];
window.setTimeout('changeURL('+(++i)+')',1000);

}
window.onload = init;
Michael Winter wrote:
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
Nov 3 '06 #5
Amy

Wait a minute maybe it does, I'll let you know.

Nov 3 '06 #6
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.

Amy wrote:
Wait a minute maybe it does, I'll let you know.
Nov 3 '06 #7
Amy

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

Nov 3 '06 #8
Amy wrote:
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[i][0];
dynLink.href = arr[i][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[i][0];
dynLink.href = arr[i][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.
Nov 3 '06 #9
Amy
Thanks, what does top posting mean?

Tried this, it isn't working.
>
function changeUrl() {
var i = Math.floor(Math.random() * arr.length);

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

setTimeout('changeUrl()', 1000);
}
Nov 6 '06 #10
Amy wrote:
Thanks, what does top posting mean?
This group's FAQ contains detailed information on posting, including a
description of top-posting.

<http://www.jibbering.com/faq/faq_notes/clj_posts.html>
> function changeUrl() {
var i = Math.floor(Math.random() * arr.length);

dynLink.innerHTML = arr[i][0];
dynLink.href = arr[i][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
Nov 6 '06 #11
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[i][0];
dynLink.href = arr[i][1];
setTimeout('changeURL()', 1000); }

Nov 6 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Michael Strorm | last post by:
Hi! I posted a message a while back asking for project suggestions, and decided to go with the idea of creating an adventure game (although it was never intended to be a 'proper' game, rather an...
3
by: valued customer | last post by:
Is there a more concise way to do something like the the desired code below? The gripe is with the try-catch syntax. It takes *way* too many lines of code to evaluate a conditional expression...
5
by: TrvlOrm | last post by:
Can any one please help me...I am new to JavaScript and I have been struggling with this code for days now and can't figure it out. I would like to get the Buttons to correspond with the action...
11
by: Lues | last post by:
Hi, I'm trying to protect some data in tables with encription (you know why, don't you ;)) I must confess that I'm not very expirienced in writing code, especially encription code. Can any...
11
by: Steve Clay | last post by:
I have a small C program for a college course. It is meant to encrypt and decrypt lower case letters and leave spaces as spaces. I can't get it to run properly as I think I have a problem in the...
36
by: Cap'n Ahab | last post by:
I have used VB3 - VB6, so learning all this OO stuff is reasonably new to me (although I looked at Java a few years ago). Anyway, I thought I would write a small class to begin with, with a...
5
by: CoreyWhite | last post by:
It is possible to use martingale probability theory to beat some games of chance. In a fair game of coin toss, where the odds reach an equilibrium of 50/50 chain reactions do occur. This can be...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.