<script type="text/javascript">
//随机取得数组中的一个元素
function Rand(){
 var arr=[11,22,33,44];
 var a=Math.floor(Math.random()*arr.length);//Math.floor(Math.random()); //Math.floor()方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。
 //alert(Math.random()*arr.length);
 //alert(a);
 alert(arr[a]);
 return arr[a];
 }
Rand() 
//数组元素随机排序:方法一
// var arrs=[11,22,33,44];
// var newArr=arrs.sort(function(){
// return Math.random()>0.5?-1:1;
// })
// alert(newArr)// sort() 方法用于对数组的元素进行排序,对数组的引用。请注意,数组在原数组上进行排序,不生成副本。
// 如果想按照其他标准进行排序,就需要提供比较函数,该函数要比较两个值,然后返回一个用于说明这两个值的相对顺序的数字。比较函数应该具有两个参数 a 和 b,其返回值如下:
// 若 a 小于 b,在排序后的数组中 a 应该出现在 b 之前,则返回一个小于 0 的值。
// 若 a 等于 b,则返回 0。
// 若 a 大于 b,则返回一个大于 0 的值。 
//数组元素随机排序:方法二
var arr1=[11,22,33,44];
var arr2=[];
//如果把一个数组所有元素for遍历出来再随机选取一个,那么选取的元素很可能是重复同一个,
//如果随机取过一个元素之后,又能把这个元素删除,那么之后再继续对数组随机取值,就不会出现重复的情况//splice 添加删除数组元素,并返回 删除的 数组元素
//如果arr1长度>0,用splice随机删除一个arr1中的元素,并把这个删除的存到一个新数组里面arr2中,那么arr2是一个作为一个新的随机生成的数组,函数返回这个数组
//alert(arr1.splice(1,1));
function a(){ //while只要指定条件为 true,循环就可以一直执行代码。
 while(arr1.length>0){
 arr2.push(arr1.splice(Math.floor(Math.random()*arr1.length),1));
 }
 alert(arr2);
}
a();
//alert(len);
</script>

 

实例:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
*{ list-style:none; text-align:center; font-size:14px; color:#fff; font-family:"微软雅黑"}
body{ list-style:none; margin:0; padding:0; border:0;}
html,body{ height:100%}
p,input,li,ul,a,span,button,div,select,p,{ display:inline-block; list-style:none; padding:0; margin:0; text-align:center;outline:none; color:#fff; font-weight:bold; font-size:14px; font-family:"微软雅黑"}
a{width:20px; height:20px; line-height:20px; display:inline-block;}
input{ background:#afa; width:260px; border:0}
input.end{ background:#cf60;}
span{ overflow:hidden; display:block; float:right;margin:0; margin-left:14px;color:#333; cursor:pointer}
div{ margin:0 auto; width:500px;height:480px; overflow:hidden; border:1px solid #fc0; /* background:url(image/a11.jpg) no-repeat center center ; */position:relative}
input{ background:#39C; border-radius:5px;width:160px; height:40px; cursor:pointer; margin:15px}
p{ display:none}
p,ul{ width:100%; height:100%; position:relative; padding:0; margin:0 }
ul li{ width:125px; height:160px; overflow:hidden; float:left; position:absolute;}
 #id0{ background:url(image/c1.jpg) no-repeat center center}
#id1{ background:url(image/c2.jpg) no-repeat center center}
#id2{ background:url(image/c3.jpg) no-repeat center center}
#id3{ background:url(image/c4.jpg) no-repeat center center}#id4{background:url(image/c5.jpg) no-repeat center center}
#id5{ background:url(image/c6.jpg) no-repeat center center}
#id6{ background:url(image/c7.jpg) no-repeat center center}
#id7{ background:url(image/c8.jpg) no-repeat center center}#id8{ background:url(image/c9.jpg) no-repeat center center}
#id9{ background:url(image/c10.jpg) no-repeat center center}
#id10{ background:url(image/c11.jpg) no-repeat center center}
#id11{ background:url(image/c12.jpg) no-repeat center center}

</style><script type="text/javascript">
window.οnlοad=function(){
 var num=12; //设置 拼图 有多少网格组成
 var div=document.getElementById("div");
 var input=document.getElementsByTagName("input")[0];
 var ul=document.createElement("ul");
 ul.setAttribute("id","ul")//给ul设置一个Id
 div.appendChild(ul);
 var ulWrap=document.getElementById("ul");
 console.log(ulWrap);
 var lis=[];
 var RandomArr=[];

 //根据拼图格数,创建对应个数的li;创建的每个Li存到一个数组中
 for(var i=0;i<num;i++){
 var li=document.createElement("li");
 //console.log(li);
 lis.push(li);
 li.innerHTML=i;//这个地方设置一个innerHtTML方面在console里面查看下面代码是否成功把这些li重新排序
 id="id"+i; 
 li.setAttribute("id",id);
 } //吧上面生成的li的新数组随机排序,
 while(lis.length>0){ 
 var arrLi=lis.splice(Math.floor(Math.random()*lis.length),1); //splice 随机 删除并返回删除的元素
 RandomArr.push(arrLi) ; //把随机删除的元素重新汇总到一个新数组中,即把原数组随机排序;//RandomArr是二维数组
 } 
 console.log(RandomArr); 

 for(var n=0;n<RandomArr.length;n++){
 node=RandomArr[n][0];

 //ulWrap.appendChild(RandomArr[n]);这举报,原因是被添加的元素是个数组,不是元素节点
 /*Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. */

 console.log(RandomArr[n][0]);//RandomArr是二维数组
 ulWrap.appendChild(node);
 //node.style.left= node.offsetWidth*n + "px";
 }}
</script>
</head>
<body>
<input type="button" value="点击开始">
<div id="div">
 <p></p>
</div>
</body>
</html>