Generate n ~ Random Number of m

Source: Internet
Author: User
Generate N-M, which contains N but does not contain an integer of M:

The first step is to calculate the M-N value, assuming that it is equal to W

Step 2 math. Random () * w

Step 3 math. Random () * w + n

Step 4 parseint (math. Random () * w + N, 10)

Generate N-M, which does not contain N but contains an integer of M :?

The first step is to calculate the M-N value, assuming that it is equal to W

Step 2 math. Random () * w

Step 3 math. Random () * w + n

Step 4 math. Floor (math. Random () * w + n) + 1

Generate N-M, excluding Integers of N and M:

The first step is to calculate the value of the m-n-2, assuming that it is equal to W

Step 2 math. Random () * w

Step 3 math. Random () * w + n + 1

Step 4 math. Round (math. Random () * w + n + 1) or math. Ceil (math. Random () * w + n + 1)

Generate a random number of N-m, including N and M:

The first step is to calculate the M-N value, assuming that it is equal to W

Step 2 math. Random () * w

Step 3 math. Random () * w + n

Step 4 math. Round (math. Random () * w + n) or math. Ceil (math. Random () * w + n)

 

Example:

Generate a random integer of 800 to 1500, including, but not

  1. 1500-800 = 700
  2. Math. Random () * 700
  3. VaR num = math. Random () * 700 + 800;
  4. Num = parseint (Num, 10 );

Only four simple steps are required.

 

Supplement:

Math. Ceil () returns the smallest integer greater than or equal to the number parameter (take the entire function), rounded up the number

Math. Floor () returns the largest integer smaller than or equal to the number parameter, and homes the number

Math. Round () returns the nearest integer to the number, rounding

 

Generate n ~ Random Number of m

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.