Help - Search - Members - Calendar
Full Version: make it random.
Forums > Resource Center > Webmasters' Corner > Webmasters' Corner Resolved Topics
OneOfTheseDayz
Does anyone know how to make a javascript random??????
mipadi
Random in what way? JavaScript can generate a random number with the Math.random() function.

Note: The number returned is not mathematically random, of course, but it's good enough to fool a human.
OneOfTheseDayz
here Ill post the code...............
mipadi
Well...I'm still not entirely sure what you're trying to make random, but I can make an attempt to parse the code. (JavaScript is not my area of expertise, but the syntax is familiar enough that I am fairly sure I know what's going on.)

CODE
var mem=[];

This is declaring an array called mem. The array is empty.

CODE
mem[mem.length]=["Anime feak","http://s15.invisionfree.com/Animeupgrade/index.php?showuser=15"]
mem[mem.length]=["Lottery","http://s15.invisionfree.com/Animeupgrade/index.php?showuser=18"]
mem[mem.length]=["Raze","http://s15.invisionfree.com/Animeupgrade/index.php?showuser=26"]

This is setting the index of the length of mem to a value--another array, to be exact, making mem a two-dimensional array (as far as I can tell). It seems to be setting the same index to a value...hrm...

CODE
var fdiv=document.getElementsByTagName("div")

This gets an array of all the DIV's in a webpage and stores it in fdiv.

CODE
for(a=0;a<fdiv.length;a++)
{
   FakeStaff='';
   if(fdiv[a].className=="maintitle" && fdiv[a].innerHTML.match("Board Statistics"))
   {
       var UsersOnline=fdiv[parseInt(a)+1]
       for(c=0;c<mem.length;c++)
       {
           UsersOnline.innerHTML+=', <a href="'+mem[c][1]+'">'+mem[c][0]+'</a>'
       }
   }
}

This steps through every DIV element on the page. If the DIV is called "maintitle" and it is within an HTML element called "Board Statistics", then the number of users online is stored in the variable usersOnline. Then, the inner for loop steps through every entry in mem and adds on a link with a specified label (from the array mem.

CODE
var ftd=document.getElementsByTagName("td")

Gets an array of every TD element on a page and stores it in the variable ftd.

CODE
for(a=0;a<ftd.length;a++)
{
   if(ftd[a].className=="pformstrip" && ftd[a].innerHTML.match("active in the past"))
   {
       var td=ftd[parseInt(a)+2]
       var beforeMemNum=td.innerHTML.split("guests, <B>")[0]+"guests, <b>"
       var afterMemNum='</b> members,'+td.innerHTML.split("</B> members,")[1]
       var MemNum=parseInt(td.innerHTML.split("guests, <B>")[1].split("</B> members,")[0])+parseInt(mem.length)
       var nMemCount=beforeMemNum+MemNum+afterMemNum
       td.innerHTML=nMemCount
   }
}

This steps through every TD element on the page. If the TD is named "pformstrip" and is within an element specified by "active in the past", then some information is pulled out of ftd and stored in the variable td. The information is then parsed to get some sort of count.

Now, what precisely are you trying to randomize?
OneOfTheseDayz
ummm. Ok so I akm trying to randomize the different members....
here I am better at example the site here at the bottom it shows different members ( 3 of these being ones that are put in through this code). I am trying to make it randomize so somtimes when you click 2 members come one time and when you click later on it shopws a different two to 3 up for instance ( I am using A, B , C to represent members) A and B are on the first time you are on later when you go back on C and A are on. Later when you go back on A, B and C are on.......
mipadi
Okay. The first thing to do is populate the array, which is best done like this:
CODE
var mem=[];

mem[0]=["Anime feak","http://s15.invisionfree.com/Animeupgrade/index.php?showuser=15"];
mem[1]=["Lottery","http://s15.invisionfree.com/Animeupgrade/index.php?showuser=18"];
mem[2]=["Raze","http://s15.invisionfree.com/Animeupgrade/index.php?showuser=26"];

This gives you a two-dimensional array with the values you desire.

Now, you want to generate two or three users to display. This is a bit tough. I'd first generate a random number of entries using Math.random():
CODE
var displayNumUsers = Math.floor(Math.random() * 3);
if (displayNumUsers < 2)
{
   count++;
}

That will give you a counter that will hold either the value 2 or 3.

Now, just loop through and display the users. First, create a variable to hold the number of users displayed, then a variable to hold the users that have been displayed.
CODE
var count = 0;
var users=[];
users[0] = -1;
users[1] = -1;
users[2] = -1;
while (count <= displayNumUsers)
{
   var user;
   var index;
   index = Math.floor(Math.random() * mem.length);
   while (index == users[0] || index == users[1] || index == users[2])
   {
       index = Math.floor(Math.random() * mem.length);
   }
   user = mem[index];
   //code for displaying user, using variable user
   count++;
   users[count - 1] = index;
}

Admittedly, this I hacked this code out pretty quick, so it's not optimized. I'm not an expert in JavaScript and I haven't tested this code, but I'm fairly confident it will work, or it can be fixed easily.
OneOfTheseDayz
ok I think you may have confused me a bit........

here is what I did.....


Maybe I skrewed up but when I put it up it stops responding............
mipadi
Try this:

CODE
var mem=[];

mem[0]=["Anime feak","http://s15.invisionfree.com/Animeupgrade/index.php?showuser=15"]
mem[1]=["Lottery","http://s15.invisionfree.com/Animeupgrade/index.php?showuser=18"]
mem[2]=["Raze","http://s15.invisionfree.com/Animeupgrade/index.php?showuser=26"]

var displayNumUsers = Math.floor(Math.random() * 3);
if (displayNumUsers < 2)
{
  count++;
}


var count = 0;
var users=[];
users[0] = -1;
users[1] = -1;
users[2] = -1;
while (count <= displayNumUsers)
{
  var user;
  var index;
  index = Math.floor(Math.random() * mem.length);
  while (index == users[0] || index == users[1] || index == users[2])
  {
      index = Math.floor(Math.random() * mem.length);
  }
  user = mem[index];
 
   var fdiv=document.getElementsByTagName("div")
   for(a=0;a<fdiv.length;a++)
   {
       FakeStaff='';
       if(fdiv[a].className=="maintitle" && fdiv[a].innerHTML.match("Board Statistics"))
       {
           var UsersOnline=fdiv[parseInt(a)+1]
           for(c=0;c<mem.length;c++)
           {
               UsersOnline.innerHTML+=', <a href="'+user[1]+'">'+user[0]+'</a>'
           }
       }
   }
   var ftd=document.getElementsByTagName("td")
   for(a=0;a<ftd.length;a++)
   {
       if(ftd[a].className=="pformstrip" && ftd[a].innerHTML.match("active in the past"))
       {
           var td=ftd[parseInt(a)+2]
           var beforeMemNum=td.innerHTML.split("guests, <B>")[0]+"guests, <b>"
           var afterMemNum='</b> members,'+td.innerHTML.split("</B> members,")[1]
           var MemNum=parseInt(td.innerHTML.split("guests, <B>")[1].split("</B> members,")[0])+parseInt(mem.length)
           var nMemCount=beforeMemNum+MemNum+afterMemNum
           td.innerHTML=nMemCount
       }
   }
 
  count++;
  users[count - 1] = index;
}
OneOfTheseDayz

Ok thanks I think the problem is that there are too many names because now when I go to my site all three of the names appear at the same time but they are random.. ( visit my site to see what I mean).......Thanks biggrin.gif
mipadi
I think it has something to do with the way the names are displayed. You might have to tweak that code; I didn't examine the structure of your board and just used what you had before, which may be wrong.
OneOfTheseDayz
OK you have me confused. By tweaking do you mean editing numbers, and by structure you mean what???
mipadi
Well, the script does something dealing with the TD and DIV elements of the board, which I didn't examine very closely. The script may not be functioning properly because of the way certain elements are added and/or modified.
OneOfTheseDayz
ok then so how would I find out if it is right or not?
mipadi
Output the values of the variables, run a few tests, find out exactly what the various loops and stuff are doing…
OneOfTheseDayz
Ok I think I hae been through every number combination know to man......... but I have been thinking that maybe a display 1 code would work????
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.