<script language="javascript"> var numme=Math.floor(Math.random()*3); </script>ใฃ้สำหรับ random หาตัวเลขที่ต้องการ จาก 0 ถึงจำนวนที่เรากำหนด
เช่นในที่นี่กำหนดเป็น 3 คือเอา 3 ไปคูณ ค่าที่ random ได้ก็จะมี 0,1,2
และ 3 เราสามารถกำหนดเป็นจำนวนที่ต้องการได้
ตัวอย่างการใช้งาน ให้ทำการ random ไฟล์ flash
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function RandomImg()
{
var timeout=0;
if(timeout) clearTimeout(timeout);
var img = new Array();
img[1] = "watch/watch1.swf";
img[0] = "watch/watch2.swf";
img[2] = "watch/watch3.swf";
seconds=Math.floor(Math.random()*3);
var me=document.getElementById("myflash").movie=img[seconds];;
timeout=setTimeout("RandomImg()",2000); //30seconds7000
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Untitled Document</title>
</head>
<body onload="RandomImg()">
<object id="myflash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"
width="130" height="130">
<param name="movie" value="watch/watch1.swf" />
<param name="quality" value="high" />
<embed src="watch/watch1.swf" quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"
width="130" height="130"></embed>
</object>
</body>
</html>