1. สุ่มสร้างตัวเลขสุ่ม 4 หลัก
<script language = "JavaScript">/*** แบบสุ่มสร้างหมายเลขสุ่ม 4 บิต*http: //www.yulu.vevb.com*/document.write (Parseint (10*Math.random ())); // เอาท์พุทแบบสุ่มจำนวนเต็ม document.write (math.floor (math.random ()*10+1)); // ฟังก์ชั่นการสุ่มอินเตอร์ของอินเทอร์ rndnum (n) {var rnd = ""; สำหรับ (var i = 0; i <n; i ++) rnd+= math.floor (math.random ()*10); ส่งคืน rnd;} document.write (rndnum (4)); // เอาต์พุตจำนวนเต็มแบบสุ่มของจำนวนสุ่มของจำนวนตัวเลขที่ระบุ </script>2. แบบสุ่มสร้างจำนวนข้อมูลที่ระบุ
1) เริ่มจาก 1 ถึงค่าใด ๆ
parseint (math.random ()*ขีด จำกัด บน+1);
2) เริ่มจากค่าใด ๆ ไปจนถึงค่าใด ๆ
<script> ParseInt (math.random ()*(ขีด จำกัด ขีด จำกัด บน+1)+ขีด จำกัด ล่าง); ฟังก์ชั่น frandomby (ต่ำกว่า, มากกว่า) {switch (arguments.length) {กรณีที่ 1: return parseint (math.random ()*ภายใต้+1); กรณีที่ 2: ส่งคืน parseint (math.random ()*(เกินกว่า-ต่ำกว่า + 1) + ต่ำกว่า); ค่าเริ่มต้น: return 0; }} document.write (Frandomby (1,100)); // อินเทอร์สแบบสุ่มของตัวเลขสุ่มในช่วงที่ระบุ </script> // โปรดค่าที่แตกต่างกันตามกฎสำหรับกล่องข้อความที่กำหนด [Extended] <script> window.onload = function () {var o = document.getElementsByTagname ('อินพุต'); o [0] .Value = Frandomby (1,10); o [1] .Value = Frandomby (11,20); o [2] .Value = Frandomby (1,100); o [3] .Value = Frandomby (51,100); } </script> 1-10: <input type = "text" /> <br /> 11-20: <input type = "text" /> <br /> 1-100: <input type = "text" /> <br /> 51-100: <input type = "text" /> <br />>3. ตัวอย่างเพิ่มเติม:
<html> <head> <title> ตัวอย่างคณิตศาสตร์ของการสร้างตัวเลขสุ่ม www.vevb.com </title> </head> <body> <body> <script language = "javascript" type = "text/javascript"> total = 0 for (i = 1; i <= 5000; i ++) {math.random (); รวม+= num} ค่าเฉลี่ย = ทั้งหมด/5000; ค่าเฉลี่ย = math.round (เฉลี่ย*1000) /1000;document.write ("<H1> ค่าเฉลี่ย:"+เฉลี่ย+"</h1>") </script> </body> </html>รหัสตัวอย่างข้างต้นสำหรับการสร้างหมายเลขสุ่ม (ช่วงที่ระบุ) ใน JS เป็นเนื้อหาทั้งหมดที่ฉันแบ่งปันกับคุณ ฉันหวังว่าคุณจะให้ข้อมูลอ้างอิงและฉันหวังว่าคุณจะสนับสนุน wulin.com มากขึ้น