Dùng Math.random() trả về số thực ngẫu nhiên trong khoảng [0, 1) (bao gồm 0, không bao gồm 1).
Ví dụ
<html>
<head>
<title>Phương thức Math.random trong JavaScript</title>
</head>
<body>
<script>
document.write("Lần 1: " + Math.random() + "<br>");
document.write("Lần 2: " + Math.random());
</script>
</body>
</html>
Mở rộng: Để random số nguyên trong [min, max]: Math.floor(Math.random() * (max - min + 1)) + min