[Javascript] Random file on page load... or at least that was the idea :(

Harry Love hlove at u.washington.edu
Thu Oct 17 14:09:11 CDT 2002


 From my understanding, the original code works like this:

Len = movies.length; // len = length of your array (number of array
items)
Today = new Date(); // Creates a new date object, stored in the "today"
variable
Today = today.getTime()/10; // Get the number of milliseconds that have
elapsed since 01/01/1970, then divide by 10, store in "today"
Temp_number = today%len; // Divide today by len, take the remainder,
store in the "temp_number" variable
Rnd = Math.round(temp_number); // Round temp_number to the nearest whole
number, store in the "rnd" variable

So, the rnd variable is loosely based on the date.  Perhaps the original
author had a reason for approaching it this way.  However, for random
numbers, the Math.random() method is the most direct.

Harry

-----Original Message-----
From: javascript-admin at LaTech.edu [mailto:javascript-admin at LaTech.edu]
On Behalf Of McCoy, Thomas
Sent: Thursday, October 17, 2002 11:39 AM
To: 'javascript at LaTech.edu'
Subject: RE: [Javascript] Random file on page load... or at least that
was the idea :(


Thanks Harry!  The "random number" was exactly what I was looking for.

Am I correct in saying that the other code was randomly selecting a
movie
based on the date and time?  (Sorry to be a bother, but I'm trying to
learn
as I go here)
 
 
Sincerely,
Thomas McCoy

-----Original Message-----
Thomas, a quick question for you: are you looking for a random number,
or a number based on the time of day?

If you want a random number (to pick a random file from your array), try
this:

// replace all of this
len = movies.length;
today=new Date();
today=today.getTime()/10;
temp_number=today%len;
rnd = Math.round(temp_number);

// with this one line
rnd = (Math.floor(Math.random() * movies.length));
_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list