<!doctype html> <html> <head> <meta charset="utf-8"> <title>数学方法随机的运用</title> </head> <body> <img src="" id="myimg"> <script> /*0<x<1;//x=Math.random(); 0<x*5<5;//x.length,0到5之间的浮点数; 0<=Math.floor(x*5)<=4;//取得x的index;*/ var arr=["a.png","b.png","c.png","d.png","e.png"]; var rand=Math.random; var tot=arr.length; var index=Math.floor(rand*tot); var myImg=document.getElementById("myimg"); myImg.src=arr[index]; </script> </body> </html>