@eihabisaac
Try this...
- function shuffle()
-
{
-
var imgval;
-
-
//CHANGE 1
-
var num = "123456789";
-
-
document.write("<table border='1'>")
-
var k=1;
-
-
for (var i=1;i<=3;i++)
-
{
-
document.write("<tr>");
-
for(var j=0;j<3;j++)
-
{
-
-
//CHANGE 2
-
imgval = num.charAt(Math.floor(Math.random() * num.length));
-
num = num.replace(imgval,'');
-
-
document.write("<td><img src='Image"+(imgval)+".bmp' </td>");
-
-
}
-
k+=3;
-
document.write("</tr>");
-
}
-
document.write("</table>");
-
-
-
}
A suggestion: Try to avoid the use of bmp format.
Using Math.round() instead of Math.floor() will help reduce repetition; it splits hairs better.
Well, I have a little disagreement with this.
I think
Math.floor() will do it more even than
Math.round()
Lets consider this mathematical example by generating randoms between 0 to 9 using each of the above functions.
Using
Math.round(),
Math.round(Math .random() * 9) will generate randoms between 0 and 9.
And Using
Math.floor(),
Math.floor(Math .random() * 10) will generate randoms between 0 and 9.
Results (all cases):
-
random() round() floor()
-
between 0-1 0 1 0
-
between 1-2 1 2 1
-
between 2-3 2 3 2
-
between 3-4 3 4 3
-
between 4-5 4 5 4
-
between 5-6 5 6 5
-
between 6-7 6 7 6
-
between 7-8 7 8 7
-
between 8-9 8 9 8
-
between 9-10 NA 9
So this shows, probability of occurrence of all the integers is equal in case of
floor(), but in case of
round(), 0 and 9 have half the probability of occurance than other integers.
So, I would recomend to use
floor() (
or ceil()) rather than
round()
Regards,
Harpreet