Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    Random removeChild()

    So hey here is a new one.

    I have a list of elements like so:

    Code:
    <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.

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Here's a simple pseudo-random number generator, if it might help:

    Code:
    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.

  • #3
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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..

  • #4
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by ubh View Post
    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.

  • Users who have thanked fside for this post:

    ubh (07-16-2009)

  • #5
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Thanks man, that wasn't so hard with that function handed off to me.

    Code:
    <!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!

  • #6
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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.

    Code:
    <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>

  • #7
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Quote Originally Posted by fside View Post
    Here's a simple pseudo-random number generator, if it might help:

    Code:
    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:
    Code:
    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:
    Code:
    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;
    }
    Trinithis

  • #8
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by Trinithis View Post
    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.

  • Users who have thanked fside for this post:

    Trinithis (07-17-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •