Generating random numbers in a specific interval in Java __java

Source: Internet
Author: User

Original address: http://blog.sina.com.cn/s/blog_59aebaa10100ct47.html

Reference Address: http://blog.csdn.net/codefunjava/article/details/44408555

Http://www.cnblogs.com/xwdreamer/archive/2012/06/13/2547426.html




Generate random numbers within a specified range
This is one of the most commonly used techniques. Programmers want to handle a multitude of business logic by random numbers, and the test process also wants to generate a large number of test cases by random numbers. The problem is often similar to:
How to randomly generate random numbers between 1~100, which contains boundary values of 1 and 100.
Or is:
How to randomly generate a random 3-bit integer.
Wait a minute......
In the case of the Java language, we observe the Nextint (int) method of its Random object and find that this method generates an integer that is randomly evaluated between 0 ~ parameters. For example (assuming Random rand = new Random ();, below):
Rand.nextint (100);
This line of code will generate a random number between the range 0~100, and interestingly, the value may be 0, but not 100. We use the middle School mathematics class to study the interval representation, expressed as: [0, 100).
So if you want to get the random number of the interval [1~100], what do you do? A little bit of your brain can be thought of: the integers within the interval [0, 100] are actually the intervals [0, 99]. Because the maximum boundary is 100, unfortunately not equal to 100, so the most likely "integer" is 99.
Since Rand.nextint (100) Gets the value of the interval [0, 99], the interval [1, 100] is obtained by adding 1 to the interval. So the code is written:
Rand.nextint (100) + 1;
Can. Run the following code, and you will get 10 values from [1, 100].
Code Snippet 3//////////////////////////////////////////////////////////////
Import Java.util.Random;
public class Test {
public static void Main (string[] args) {
Random rand = new Random ();
for (int i=0; i<10; i++) {
System.out.println (Rand.nextint (100) + 1);
}
}
}
Code Snippet 3 End/////////////////////////////////////////////////////////
Compile run, the output is:
81
64
31
86
56
14
45
57
28
90
Run multiple times, the result is different each time, but the value must be between 1 and 100, may appear 1 and 100.
Similarly, it's easy to know if you want to get a random two-bit integer, the code writes:
Rand.nextint (90) + 10;
You must be surprised why it is so written. In fact, the number 90 in the Nextint () method as a parameter indicates: the number of probabilities that you want to generate for all values of the random number (in this proposition, the two-bit integer takes a value of [10, 99], a total of 90 digits); The following number 10 is added to indicate the minimum value of the interval.
You can verify that, according to this understanding, [1, 100] of the random number, is not supposed to be written rand.nextint (100) + 1. Never interpret parameter 100 as the maximum value. Only the interval [1, 100] is exactly starting from 1, so the maximum value and the probability of the number is exactly 100.
So
The code that generates a random three-digit number is:
Rand.nextint (900) + 100;
The code for generating the random value in the interval [64, 128] is:
Rand.nextint (65) + 64;
How is the number of value possibilities calculated? The maximum value, of course, is the minimum value of +1, so the final formula is as follows:
For Java
int randnumber = Rand.nextint (max-min + 1) + MIN; Randnumber will be assigned to a random number within the range of MIN and MAX
Under the Microsoft platform, because the. Net Framework provides a way to generate integers in fixed intervals, the formula is:
For C #
int randnumber = rand. Next (MIN, MAX + 1); Randnumber will be assigned to a random number within the range of MIN and MAX




Package Edu.sjtu.erplab.io;

Import Java.util.Random;

public class Randomtest {public
    static void Main (string[] args) {
        int max=20;
        int min=10;
        Random Random = new Random ();

        int s = random.nextint (max)% (max-min+1) + min;
        System.out.println (s);
    }



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.