Combining JavaScript carry-out and random generation

Source: Internet
Author: User

Combining JavaScript carry-out and random generation

1. carry round integer

First, let's take a look at several small examples of the padding trade-off in JS:

// JS carry trade-offs // rounded up alert (Math. ceil (25.9); // print: 26 alert (Math. ceil (25.5); // print: 26 alert (Math. ceil (25.1); // print: 26 // round down alert (Math. floor (25.9); // print: 25 alert (Math. floor (25.5); // print: 25 alert (Math. floor (25.1); // print: 25 // standard rounding alert (Math. round (25.9); // print: 26 alert (Math. round (25.5); // print: 26 alert (Math. round (25.1); // print: 25 // round //-----------------------------------------------------------------------------------
From the above example, we can easily see the effects of different trade-off functions:

Rounded up. If the fractional part is greater than 0, after "rounded up", the number of integers plus 1 (for example, 25.1, 26) excluding the decimal part is obtained ).
Round down, regardless of the decimal part, all are directly rounded up. Only the remaining integer part of the decimal part is removed (for example, 25.9, 25 ).

Standard round, rounding, decimal part less than 0.5 rounded off decimal point, only an integer (such as 25.2, take 25); fractional part greater than or equal to 0.5, take the integer part after the decimal part is truncated + an integer of 1 (for example, 25.7, 26 ).

Ii. generate random numbers

// Generate a random number // (0, 1): generate a random number between 0 and 1, excluding one random number between 0 and 1. (Display Method: alert () is displayed in a pop-up window) alert (Math. random (); // random // 10 random numbers: 0 to 1, excluding 0 and 1. (Display Method: document. write () is displayed on the page) for (var I = 0; I <= 10; I ++) {document. write (Math. random (); document. write ('
');}//-----------------------------------------------------------------------

 

Iii. Combination and Application of carry round rounding and Random Number Generation

// A random number of 10 for (var I = 0; I <= 10; I ++) {document. write (Math. floor (Math. random () * 10 + 1); document. write ('
');} // --------------------------------------------------------------------- // A random number of 10 for (var I = 0; I <= 10; I ++) {document. write (Math. round (Math. random () * 10); document. write ('
');} // A random number of 10 for (var I = 0; I <= 10; I ++) {document. write (Math. floor (Math. random () * 10 + 5); document. write ('
');} // --------------------------------------------------------------------- // A random number of 10 for (var I = 0; I <= 10; I ++) {document. write (Math. floor (Math. random () * (10-5 + 1) + 5); document. write ('
');} // ------------------------------------------------------------------------- // Print [10 ~ The Random Number of 100] 10 documentDataArray (10,100, 10); print: One of the examples is, "///// sum //// generate a random array and generate num random numbers (including the number start and end) between [start and end) //// sumfunction select (start, end, num) {// defines an Array var item = new Array (); // a random number is generated cyclically for (var I = 0; I '); }}// ------------------------------------------------------------------------- // Print (10 ~ 100) a random number of 10 documentDataArray (10,100, 10); function documentDataArray (start, end, num) {// One of the examples is, 89,88 "a total of 10 var items = new Array (); item = select (start, end, num); for (var j = 0; j ');} // Sum // generate a random array and generate num random numbers (excluding the number start and end) between (start, end .) //// Sumfunction select (start, end, num) {// defines an Array var item = new Array (); // a random number is generated cyclically for (var I = 0; I The preceding random number can be used for simple verification codes. Of course, there are more convenient and rich ways to generate verification codes. The regular expression is used to generate 0 ~ 9, ~ Z, ~ The random character of Z.



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.