Java method to get the specified number of intervals

  • 2020-04-01 03:52:49
  • OfStack

This article illustrates a Java method for obtaining a specified interval number. Share with you for your reference. The details are as follows:

In the writing of Java program a lot of times have used the random number, but has always been to use a check, this time to a summary, the generation of random number in Java to make it clear, after the direct reference to their own this article is good.

There are two classes in Java that generate random Numbers:

Java. Util. Random and Java. Math. The Random

The java.util.Random class in the Java utility class library provides methods for generating various types of Random Numbers. It can generate random Numbers of types such as int, long, float, double, and Goussian. This is where it differs most from the method Random() in java.lang.math, which only produces Random Numbers of type double.
The method in class Random is simple, with only two constructors and six general methods.

Construction method:

(1) public Random ()

(2) public Random (seed)

Random() USES the current time, system.currenttimemillis (), as the generator's seed, and Random(long seed) USES the specified seed as the generator's seed.

Java requires a base value seed to generate random Numbers, and in the first method the base value defaults to the system time as seed.

General methods:

(1)public synonronized void setSeed(long seed)

The method is to set the base value, seed.

(2) public int nextInt ()

The method is to generate an integer random number.

(3) public long nextLong ()

The method is to generate a long random number.

(4) public float nextFloat ()

The method is to generate a Float type random number.

(5) public double nextDouble ()

This method produces a Double random number.

(6) public synchronized double nextGoussian ()

This method produces a double Goussian random number.

If two Random objects use the same seed (say, both 100) and call the same function in the same order, they return exactly the same value. As shown in the following code, the output of the two Random objects is identical

Specifies a random number in a range

The random number is controlled within a range, using the modulus operator %


import java.util.*;
 class TestRandom {
  public static void main(String[] args) {
   Random random = new Random();
   for(int i = 0; i < 10;i++) {
    System.out.println(Math.abs(random.nextInt())%10);
   }
  }
}

The random Numbers obtained are both positive and negative. Math.abs is used to make the range of obtained data non-negative
Gets a non-repeating random number within the specified range


import java.util.*;
class TestRandom {
  public static void main(String[] args) {
    int[] intRet = new int[6]; 
    int intRd = 0; //Store random Numbers
    int count = 0; //Record the number of random Numbers generated
    int flag = 0; //Whether the flag has been generated
    while(count<6){
      Random rdm = new Random(System.currentTimeMillis());
      intRd = Math.abs(rdm.nextInt())%32+1;
      for(int i=0;i<count;i++){
       if(intRet[i]==intRd){
        flag = 1;
        break;
       }else{
        flag = 0;
       }
      }
      if(flag==0){
       intRet[count] = intRd;
       count++;
      }
   }
   for(int t=0;t<6;t++){
    System.out.println(t+"->"+intRet[t]);
   }
  }
}

You can also have nextFloat and so on, all kinds of primitive types
Math. The random can also
Let's say you want a random number between 0 and 10
You could write something like this:

(int)(Math.random()*10);

JAVA generates a range of random Numbers

Generate the number between min-max


Random random=new Random();
int top = random.nextInt(maxtop)%(maxtop-mintop+1) + mintop;

Another implementation principle:


Math.round(Math.random()*(Max-Min)+Min)
long Temp; //Cannot be set to int, must be set to long
//Generate random Numbers from 1000 to 9999
Temp=Math.round(Math.random()*8999+1000);

I hope this article has been helpful to your Java programming.


Related articles: