Java List Random Item newRandom(int size, int price, List oldRandomList)

Here you can find the source of newRandom(int size, int price, List oldRandomList)

Description

new Random

License

Apache License

Declaration

public static String newRandom(int size, int price, List<String> oldRandomList) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static String newRandom(int size, int price, List<String> oldRandomList) {
        //long start = System.currentTimeMillis();
        if (size > 500) {
            return newRandomV2(size, price, oldRandomList);
        }/*from  ww w  .jav  a 2 s . co m*/
        Map<String, Integer> randomMap = new HashMap<String, Integer>();
        int r = 10000000;
        for (int i = 1; i <= price; i++) {
            randomMap.put(String.valueOf(r + i), new Integer(0));
        }
        for (int i = 0; i < oldRandomList.size(); i++) {
            randomMap.put(oldRandomList.get(i), new Integer(1));
        }
        //System.out.println("size="+randomMap.size());
        //long mmmm = System.currentTimeMillis();
        //long secs = (mmmm-start)/1000;
        //System.out.println(secs);
        List<String> ranList = new ArrayList<String>();
        for (int j = 0; j < size; j++) {
            if (randomMap.size() > 0) {
                int range = randomMap.size() - oldRandomList.size() - j;
                if (range < 1) {
                    range = 1;
                }
                int ran = new Random().nextInt(range);
                int max = Math.min(ran + oldRandomList.size() + size, randomMap.size());
                //System.out.println("ran="+ran+",max="+max);
                String[] keys = new String[randomMap.size()];
                randomMap.keySet().toArray(keys);
                for (int k = 0, x = 0; k < max; k++) {
                    String key = keys[k];
                    Integer flag = randomMap.get(key);
                    if (flag == 0) {
                        if (x == ran) {
                            //System.out.println("x="+x+",ran="+ran);
                            ranList.add(key);
                            randomMap.put(key, new Integer(1));
                            break;
                        }
                        x++;
                    }
                }
            }
        }
        return ranList.toString().replaceAll("\\s+", "").replace("[", "").replace("]", "");
    }

    public static String newRandomV2(int size, int price, List<String> oldRandomList) {
        Map<String, Integer> randomMap = new HashMap<String, Integer>();
        int r = 10000000;
        for (int i = 1; i <= price; i++) {
            randomMap.put(String.valueOf(r + i), new Integer(0));
        }
        for (int i = 0; i < oldRandomList.size(); i++) {
            randomMap.put(oldRandomList.get(i), new Integer(1));
        }
        List<String> ranList = new ArrayList<String>();
        String[] keys = new String[randomMap.size()];
        randomMap.keySet().toArray(keys);
        Integer flag = 0;
        String key = null;
        for (int i = 0, k = 0; i < keys.length; i++) {
            key = keys[i];
            flag = randomMap.get(key);
            if (flag == 0) {
                ranList.add(key);
                randomMap.put(key, new Integer(1));
                k++;
            }
            if (k == size) {
                break;
            }
        }
        return ranList.toString().replaceAll("\\s+", "").replace("[", "").replace("]", "");
    }
}

Related

  1. getRandomIdxList(int minIdx, int maxIdx)
  2. getRandomIntBetween(int min, int max, List excludeList)
  3. listChooseOne(List list)
  4. makeListOfIntegersInRange(Integer size, Integer maxOfRange)
  5. makeRandomList(int arraySize, boolean unique)
  6. newRandomIntegerList(int size)
  7. newRandomStringList(int size, String... strings)
  8. newRandomV1(int size, int price, List oldRandomList)
  9. nextElement(List list)