Hàm Rnd VBScript


❮ Toàn bộ tài liệu tham khảo VBScript

Hàm Rnd trả về một số ngẫu nhiên. Số luôn nhỏ hơn 1 nhưng lớn hơn hoặc bằng 0.

Cú pháp

Rnd[(number)]

Parameter Description
number Optional. A valid numeric expression

If number is:

  • <0 - Rnd returns the same number every time
  • >0 - Rnd returns the next random number in the sequence
  • =0 - Rnd returns the most recently generated number
  • Not supplied - Rnd returns the next random number in the sequence

Các ví dụ

ví dụ 1

Một số ngẫu nhiên:

<%

response.write(Rnd)

%>

Lưu ý rằng bạn sẽ nhận được cùng một số mỗi lần. Để tránh điều này, hãy sử dụng câu lệnh Randomize như trong Ví dụ 2

Đầu ra của đoạn mã trên sẽ là:

0.7055475

Ví dụ 2

Để tránh nhận được cùng một số mọi lúc, như trong Ví dụ 1, hãy sử dụng câu lệnh Randomize:

<%

Randomize
response.write(Rnd)

%>

Đầu ra của đoạn mã trên sẽ là:

0.4758112

Ví dụ 3

Đây là cách tạo số nguyên ngẫu nhiên trong một phạm vi nhất định:

<%

Dim max,min
max=100
min=1
Randomize
response.write(Int((max-min+1)*Rnd+min))

%>

Đầu ra của đoạn mã trên sẽ là:

71

❮ Toàn bộ tài liệu tham khảo VBScript