Use the js Math. random () function to generate random numbers between n and m.

Source: Internet
Author: User

Use the js Math. random () function to generate random numbers between n and m.

Abstract:

This article explains how to use js to generate random numbers between n and m. The main purpose is to prepare for later JavaScript code generation.

The Math. random () function returns a pseudo-random number between 0 and 1, which may be 0, but always less than 1)

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

Copy codeThe Code is as follows:
1500-800 = 700
Math. random () * 700
Var num = Math. random () * 700 + 800;
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


How can I use the C function to generate a pseudo-random number between m and n?

An example is available on msdn.
# Include <stdlib. h>
# Include <stdio. h>
# Include <time. h>

Void SimpleRandDemo (int n) // It is easy to generate n random numbers.
{
// Print n random numbers.
Int I;
For (I = 0; I <n; I ++)
Printf ("% 6d \ n", rand ());
}

Void RangedRandDemo (int range_min, int range_max, int n) // generate n random numbers between range_min and range_max
{
// Generate random numbers in the half-closed interval
// [Range_min, range_max). In other words,
// Range_min <= random number <range_max
Int I;
For (I = 0; I <n; I ++)
{
Int u = (double) rand ()/(RAND_MAX + 1) * (range_max-range_min)
+ Range_min;
Printf ("% 6d \ n", u );
}
}

Int main (void)
{
// Seed the random-number generator with the current time so that
// The numbers will be different every time we run.
Srand (unsigned) time (NULL ));

SimpleRandDemo (10 );
Printf ("\ n ");
RangedRandDemo (-100,100, 10 );
}

Use the random method of the math object to generate a random value of 100 ~ A random integer between 999 and more than 10 digits in Javascript

Function random_num (){
Var num = Math. random () * 999;
If (num <100 ){

Random_num ();

} Else {
Return num; // Random Number

}

}

Var _ num = random_num ();
Var shortwei = _ num % 100; // 10
Var shiwei = (_ num-baiwei) % 10; // hundreds of digits

Related Article

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.