Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random shuffle tables

Status
Not open for further replies.

loll

Technical User
Jul 19, 2002
4
US
Hi,

I have a table with on row and 6 cells.

Is there a way using javascript to randomly shuffle the contents of these tables around without reloading the page.

eg:
<table><TR><TD>A</td><td>b</td><td>C</td></tr></table>

then click a link and it becomes:
<table><TR><TD>B</td><td>C</td><td>A</td></tr></table>

I have seen this done with images, tho I dont know enough javascript to know how its done.

I am trying to do it with tables first if possible so that there is less image loading on the page, but if I have to change into using images i will.

Any help on making a random shuffle would be greatly appreciated.
 
This FAQ could be a start:


It re-orders row data instead of column elements.
It re-orders rather than randomly supplying values
.

Please give me a little more information on your data.

I'm not sure if the data is just re-ordered, or selected from a long list at random, whether duplicate selections are okay, etc. etc.

In my mind I'm thinking about a deck of cards and you want to choose say 5 out of 52 with no duplicates.

It also depends if you want the re-ordering to be done onnthe client (without a round-trip to the server) or whether server-side is required (say to gather current data from a list before randomly selecting).
 
Hi,
this script does more or less what you want, but has a bug, which I can't figure out.
I'm trying to create an array of random numbers through brute force repitition (there must be a better way to get non-repeating random numbers) and for some reason, occasionally the 2nd number repeats the first.Can't seem to figure it out.
Anyway, copy this page and name it &quot;rand.html&quot; and it will make a table w/ random arrangement of &quot;a&quot;,&quot;b&quot;, or &quot;c&quot;. You can adapt it to your particular needs.


<html>
<head>
<title>random table data
</title>
<style type=&quot;text/css&quot;>
table{
font-family:verdana,sans serif;
font-size:36px;
}
</style>
<script language=&quot;javascript&quot;>
function randNum(value){
return Math.floor(Math.random(10)*value);
}
index=0;
var x=new Array(3)
function makeSequence(){
x[index]=randNum(x.length);
index++;
x[index]=randNum(x.length);
if(x[index]==x[0]){
x[index]=randNum(x.length);
}
index++;
x[index]=randNum(x.length);
while(x[index]==x[0]||x[index]==x[1]){
x[index]=randNum(x.length);
}
index=0;
}
makeSequence()
var cellData=[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;];
var htmStr='<table align=center border=0><tr>';
for(var i=0;i<x.length;i++){
htmStr+='<td>'+cellData[x]+'</td>';
}
htmStr+='</tr></table>';
</script>
</head>
<body>
<a href=&quot;rand.html&quot;>do it again</a>
<script language=&quot;javascript&quot;>
document.write(htmStr);
document.close();
</script>
</body>
</htmll>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top