Java Random Int randomCommon(int min, int max, int n)

Here you can find the source of randomCommon(int min, int max, int n)

Description

random Common

License

Open Source License

Declaration

public static int[] randomCommon(int min, int max, int n) 

Method Source Code

//package com.java2s;

public class Main {

    public static int[] randomCommon(int min, int max, int n) {
        if (max == min + n) {
            return null;
        }/*from   w w  w.  ja v a2 s . c o  m*/

        if (n > (max - min + 1) || max < min) {
            return null;
        }
        int[] result = new int[n];
        int count = 0;
        while (count < n) {
            int num = (int) (Math.random() * (max - min)) + min;
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if (num == result[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                result[count] = num;
                count++;
            }
        }
        return result;
    }
}

Related

  1. randomByteArray(int length)
  2. randomByteArray(int size, byte from, byte to)
  3. randomByteAsInt()
  4. randomBytes(int size)
  5. randomBytes(int size)
  6. randomCommonStr(int min, int max, int n)
  7. randomDoubleMatrix(int rows, int columns)
  8. randomFloatInInterval(float lower, float upper)
  9. randomHex(int length)