HighDots Forums  

Randomize HTML Table Rows from JavaScript

Javascript JavaScript language (comp.lang.javascript)


Discuss Randomize HTML Table Rows from JavaScript in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Jorge
 
Posts: n/a

Default Re: Randomize HTML Table Rows from JavaScript - 07-01-2008 , 10:38 PM






On Jul 2, 2:10*am, SAM <stephanemoriaux.NoAd... (AT) wanadoo (DOT) fr.invalid>
wrote:
Quote:
Jorge a écrit :
20.49/100 ms (SAM)

Ha Yes ... not so good :-(
:-)

Quote:
9.97/100 ms (Thomas)

this one when I tried it re-display the original ordered table with a
step of 4

9.09/100 ms (Jorge)

Bon sang ! Mais bien sûr !
the famous appendChild that doesn't only add an element but can also move it

Does this also return to the initial position every 4 times ?
__Hopefully__ no : http://tinyurl.com/68hscr

--Jorge.


Reply With Quote
  #12  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Randomize HTML Table Rows from JavaScript - 07-02-2008 , 04:42 AM






Jorge wrote:
Quote:
Thomas 'PointedEars' Lahn wrote:
sanju wrote:
I have html table and this table contains 10 Rows and 2 column, I want
every time this HTML page is called by the user to view the rows
Randomly.
How can I do this from JavaScript?
Quick hack:

/**
* @return A pseudo-random IEEE-754 double in the interval
* <tt>[0, 1)</tt>.
* @type number
*/
function prng()
{
var r = Math.random();

// Opera bug workaround
if (r == 1) r = 0;

return r;
}

/**
* Returns a pseudo-random integer value in the interval
* <tt>[0, top)</tt>, or in the interval <tt>[bottom, top)</tt
* if <code>bottom</code> was provided.
*
* @param top : number
* @param bottom : optional number
* @return pseudo-random integer value in the specified interval
* @type number
*/
function prng_int(top, bottom)
{
if (!bottom) bottom = 0;
return Math.floor(prng() * (top - bottom)) + bottom;
}

/**
* Sorts the rows of the first table body of the document randomly.
*/
function bodyLoad()
{
// get table object reference
var t = ...
if (t)
{
// get table body object reference
var tbody = (t.tBodies || [])[0];
if (tbody)
{
for (var rows = tbody.rows, len = rows.length, i = len; i--
{
tbody.insertBefore(rows[i], rows[prng_int(len)]);
}
}
}
}

body onload="bodyLoad()"

Hmmm, this code shuffles the rows in just 4 lines :

var tbody= (document.getElementById('myTable')).tBodies[0],
rows= tbody.rows, i;
for (i= rows.length; i; i--) {
tbody.appendChild(rows[Math.floor((i)*Math.random())]);
}

And runs ~10% faster : http://tinyurl.com/56g37t

20.49/100 ms (SAM)
9.97/100 ms (Thomas)
9.09/100 ms (Jorge)
Obviously your different solution is not equivalent to mine, so these
benchmark results are void for comparing appendChild() and insertBefore().
What happens if you do the same feature-testing, lose extra parentheses,
and implement the same Opera PRNG fix?


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.