Java ThreadLocalRandom randomNums(int min, int max, int count)

Here you can find the source of randomNums(int min, int max, int count)

Description

random Nums

License

Open Source License

Declaration

public static List<Integer> randomNums(int min, int max, int count) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

public class Main {

    public static List<Integer> randomNums(int min, int max, int count) {
        if (max - min + 1 > count) {
            count = max - min + 1;//from  w w  w .ja  va  2s  .  c o m
        }
        Set<Integer> nums = new LinkedHashSet<Integer>();
        while (nums.size() < count) {
            nums.add(randomNum(min, max));
        }
        List<Integer> list = new ArrayList<Integer>(nums);
        return list;
    }

    public static int randomNum(int min, int max) {
        if (min < 0) {
            int i = nextInt(max + 1 + Math.abs(min));
            return min + i;
        } else {
            int i = nextInt(max + 1 - min);
            return min + i;
        }
    }

    public static int nextInt(int val) {

        return nextInt(val, true);
    }

    public static int nextInt(int val, boolean isAbs) {
        int r = (int) (ThreadLocalRandom.current().nextDouble() * val);
        if (isAbs) {
            return Math.abs(r);
        }
        return r;
    }
}

Related

  1. randomInteger(int min, int max)
  2. randomIntegerList(int min, int max, int minLength, int maxLength)
  3. randomLongs(final long count, final long min, final long max)
  4. randomLongsNonDuplicates(final long count, final long min, final long max)
  5. randomNum(int min, int max)
  6. randomPer(long num, long divisor)
  7. randomReal(float range)
  8. randomSeed()
  9. randomString()