Home  >  Article  >  Web Front-end  >  JavaScript random numbers and advanced applications with vbscript(asp) random number summary_javascript skills

JavaScript random numbers and advanced applications with vbscript(asp) random number summary_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:08:201561browse
1. It is the javascript random number function Math.random()
Generate a random number in a specified range
The Math.random() method has no parameters and returns a random number between 0 and 1. If To generate a random
number between 0~n, you can use the following format:

Math.floor(Math.random()*n)
To generate a random number between m~n Random numbers can be used:
Math.floor(Math.random()*n) m

The following is the application:

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

javascript Random Number Advanced Application

①Since the creation of JavaScript, many browsers have built-in random number generation methods. For example:
var number = Math.random();
This method produces a floating point number between 0 and 1.
② Random numbers can also be generated based on time. For example:
var now=new Date();
var number = now.getSeconds();
This will produce an integer from 0 to 59 based on the current time.
var now=new Date();
var number = now.getSeconds()C;
This will produce an integer from 0 to 42 based on the current time.
③Here is an excellent random number generator program that can be used in many fields. The code is as follows:




2. vbscript (asp) random number

vbs generates random numbers
A random number without a specified range The code is as follows:


Function GetRandomize(numstr)
Randomize
GetRandomize = Int((numstr * Rnd) 1)
End Function


Generate a random number in the specified range The code is as follows:


Function MyRnd(NumMin,NumMax)
Randomize
MyRnd = Int(Rnd * (NumMax - NumMin 1)) NumMin
End Function


Here are some example codes for js random numbers The code is as follows:


<script> <br>//Declare a random number variable, the default is 1 <br>var GetRandomn = 1; <br>//Function to get a value in a random range<br>function GetRandom(n){GetRandomn=Math.floor(Math.random()*n 1)} <br>//Start calling and get a 1 -100 random number<br>GetRandom("100"); <br>//Output view<br>document.write(GetRandomn) <br></script>

Is it quite concise? What about the code? GetRandomn is a random number variable and can be called at will.
Let’s take a random display special effect code:
<script> <br>//Start now, first get a random number from 1 to 3<br>GetRandom("3"); <br>/ /Call items that match random numbers <br>switch(GetRandomn){ <br>case 1: <br>document.write("When the random number is 1, display the script home") <br>break; <br>case 2: <br>document.write("When the random number is 2, www.jb51.net is displayed") <br>break; <br>} <br></script>


Demo code:

[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute ]<script> var GetRandomn = 1; function GetRandom(n){GetRandomn=Math.floor(Math.random()*n+1)} GetRandom("2"); switch(GetRandomn){ case 1: document.write("当随机数为1则显示脚本之家") break; case 2: document.write("当随机数为2则显示www.jb51.net") break; } </script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn