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

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

Description

random Common Str

License

Open Source License

Declaration

public static String randomCommonStr(int min, int max, int n) 

Method Source Code

//package com.java2s;

public class Main {
    public static String randomCommonStr(int min, int max, int n) {
        int[] arr = randomCommon(min, max, n);
        String tmp = "";
        for (int i = 0; i < arr.length; i++) {
            tmp += arr[i] + "";
        }//from  w  w  w.j  ava2s  . c  o  m
        return tmp;
    }

    public static int[] randomCommon(int min, int max, int n) {
        if (max == min + n) {
            return null;
        }

        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 size, byte from, byte to)
  2. randomByteAsInt()
  3. randomBytes(int size)
  4. randomBytes(int size)
  5. randomCommon(int min, int max, int n)
  6. randomDoubleMatrix(int rows, int columns)
  7. randomFloatInInterval(float lower, float upper)
  8. randomHex(int length)
  9. randomHexOfInt(int min, int max)