Java Random Number genRandomNumList(int num, int count)

Here you can find the source of genRandomNumList(int num, int count)

Description

gen Random Num List

License

Open Source License

Declaration

public static List<Integer> genRandomNumList(int num, int count) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Main {

    public static List<Integer> genRandomNumList(int num, int count) {
        if (num < count) {
            return Collections.EMPTY_LIST;
        }/*  w  w w .  ja v  a 2s  . co m*/

        List<Integer> result = new ArrayList<Integer>();
        Map<Integer, String> containMap = new HashMap<Integer, String>();
        Integer temp = null;
        Random random = new Random();
        for (int i = 0; i < count;) {
            temp = random.nextInt(num);
            if (!containMap.containsKey(temp)) {
                containMap.put(temp, null);
                result.add(temp);
                i++;
            }
        }

        return result;
    }
}

Related

  1. genRandomNumber()
  2. genRandomNumber(int begin, int end)
  3. genRandomNumber(int digitNumber)
  4. genRandomNumber(int minNumber, int maxNumber, int digits)
  5. genRandomNumberString(int numberCount)
  6. getChance(Number number)
  7. getDocumentNumber()
  8. getFlightNumber()
  9. getNextRandomNumber()