...

View Full Version : Random removeChild()



ubh
07-16-2009, 09:57 PM
So hey here is a new one.

I have a list of elements like so:


<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
<li>Six</li>
</ul>

Exactly how would one go about removing all except one of the above LI elements? Also each page refresh the left over LI element must be a randomly left over element.

The last part is the one I am getting stuck on... how to make sure the left over LI element is random each time.

Any help or study material is greatly appreciated right now.

fside
07-16-2009, 10:02 PM
Here's a simple pseudo-random number generator, if it might help:




PRNGx function(x){
var nd = new Date();
return Math.ceil( ( (nd.getTime()*9301+49297+(Math.random()*x)) % 233280) / (233280*1.0) *x);
};



PRNGx(5) should give you any one of six random numbers.

ubh
07-16-2009, 10:18 PM
Well in that there is another condition.

I want to run this function over many pages... all pages will have an unknown amount of elements, so the six I have in my above post if for purely example only.

Grr this is going to be a fun one..

fside
07-16-2009, 10:22 PM
all pages will have an unknown amount of elements

You have to identify them somehow. Then you can count them.

If you have multiple lists, then use an "id" or a unique class for the UL or each LI. I'd go with an "id" for the UL. Then just .getElementsByTagName (for LI) under that, and see how many there are. Then put that number into the pseudo-random function.

ubh
07-16-2009, 10:45 PM
Thanks man, that wasn't so hard with that function handed off to me.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<ul id="lists">
<li>Test 0</li>
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
<li>Test 4</li>
<li>Test 5</li>
<li>Test 6</li>
<li>Test 7</li>
<li>Test 8</li>
</ul>
<script type="text/javascript">

var length = document.getElementById("lists").getElementsByTagName("li").length;
randomHunt(length);

function randomHunt(x){
var nd = new Date();
var randNode = Math.ceil( ( (nd.getTime()*9301+49297+(Math.random()*x)) % 233280) / (233280*1.0) *x);
var parent = document.getElementById("lists");
var element =document.getElementById("lists").getElementsByTagName("li")[randNode];
document.getElementById("lists").innerHTML = "";
parent.appendChild(element);
};
</script>
</body>
</html>


Thanks again m8!

ubh
07-16-2009, 10:58 PM
Slight change in the script here.

Because browsers see the first element as 0 not 1 you must deduct a count of one from the randNode var.



<script type="text/javascript">

var length = document.getElementById("lists").getElementsByTagName("li").length;
randomHunt(length);

function randomHunt(x){
var nd = new Date();
var randNode = Math.ceil( ( (nd.getTime()*9301+49297+(Math.random()*x)) % 233280) / (233280*1.0) *x);
var parent = document.getElementById("lists");
var element =document.getElementById("lists").getElementsByTagName("li")[randNode - 1];
document.getElementById("lists").innerHTML = "";
parent.appendChild(element);
};
</script>

Trinithis
07-16-2009, 11:23 PM
Here's a simple pseudo-random number generator, if it might help:




PRNGx function(x){
var nd = new Date();
return Math.ceil( ( (nd.getTime()*9301+49297+(Math.random()*x)) % 233280) / (233280*1.0) *x);
};



PRNGx(5) should give you any one of six random numbers.

Why go through all that trouble?

I would just do this:


function randN (n) {
return Math.floor (Math.random () * n);
}

Just note that this is stylistically different in that it is [0, N-1] over [1, N].

For kicks:


function rand (a, b) {
if (b === undefined) {
b = a;
a = 0;
}
if (b < a) {
var x = a;
a = b;
b = x;
}
return Math.floor (Math.random () * (b - a)) + a;
}

fside
07-17-2009, 03:40 AM
Why go through all that trouble?


The nature of pseudo-random number generation is that they are not truly random sequences. I got this formula years ago in the e-mail list days. And the professor seemed to believe it produced a more random pseudo-random number. In this case, you could probably just do as you suggest. But it's just as easy to have a function. I use it to dynamically create unique ids on a page that can number in the hundreds, depending on what features I'm adding.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum