JS generates random numbers

Source: Internet
Author: User
1 < Script >
2 Document. Write (parseint (10 * Math. Random ())); // Output 0 ~ A random integer between 10
3
4 Document. Write (math. Floor (math. Random () * 10 + 1 )); // Output 1 ~ A random integer between 10
5
6 Function Rndnum (n ){
7 VaR RND = "";
8 For ( VaR I = 0; I <n; I ++)
9 RND + = math. Floor (math. Random () * 10 );
10 Return RND;
11 }
12 Document. Write (rndnum (4 )); // Returns the random integer of a random number at the specified number of digits.
13
14
15
16 References:
17
18 1. Starting from 1 to any value
19 Parseint (math. Random () * Upper Limit + 1 );
20
21 2. From any value to any value
22 Parseint (math. Random () * (upper limit-lower limit + 1) + lower limit );
23 Function Frandomby (under, over ){
24 Switch (Arguments. Length ){
25 Case 1: Return Parseint (math. Random () * under + 1 );
26 Case 2: Return Parseint (math. Random () * (over-under + 1) + under );
27 Default : Return 0;
28 }
29 }
30 Document. Write (frandomby (1,100 )); // Output random Integers of random numbers within the specified range
31 </ Script >
32
33 // Pay different values for the specified text box according to the Rules [extended]
34 < Script >
35 Window. onload = Function (){
36 VaR O = Document. getelementsbytagname ('input ');
37 O [0]. value = frandomby (1, 10 );
38 O [1]. value = frandomby (11,20 );
39 O [2]. value = frandomby (1,100 );
40 O [3]. value = frandomby (51,100 );
41 }
42 </ Script >
43 1-10: < Input Type = "Text"   /> < BR />
44 11-20: < Input Type = "Text"   /> < BR />
45 1-100: < Input Type = "Text"   /> < BR />
46 51-100: < Input Type = "Text"   /> < BR />
47
48
49
50
51 < Html >
52 < Head >
53 < Title > Math </ Title >
54 </ Head >
55 < Body >
56 < Script Language = "JavaScript" Type = "Text/JavaScript" >
57 Total = 0
58 For (I = 1; I <= 5000; I ++)
59 {Num = math. Random ();
60 Total + = num
61 }
62 Average = total/5000
63 Average = math. Round (average * 1000)/1000
64 Document. Write (" 65 </ Script >
66 </ Body >
67 </ Html >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.