没有两个连续对象的随机选择序列相同

时间:2013-11-29 11:09:34

标签: java algorithm random random-seed

我想编写一个函数,随后可以使用整数参数(从1到100开始)调用该函数,该函数随机给我一个0,1或2的整数,但在行中从不相同。

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

public class UniqueRandomObjectSelector {

    //Goal is to select objects from a bucket randomly with teh condition that 
    //no two selections in a row would be same
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(i + "th random Object is " + selectObject(i) + " ");
        }
    }

    //Pick the object from the pool of 3 . e.g. the bucket contains the numbers 1, 2 and 3
    private static int PickObject(int j) {
        Random rand = new Random(getSeed());
        //Find i-1 wala number
        for (int i = 1; i < j; i++) {
            rand.nextInt(3);
        }
        return rand.nextInt(3);
    }

    //Fixed seed so that random number generation sequence is same
    static long getSeed() {
        return 11231;
    }

    static int selectObject(int index) {
        //Holds the sequence of Objects
        List<Integer> list = new ArrayList<>();
        int prev = -999;
        int i = 1;
        //Keep generating the sequence till we have the requested index of objects 
        while (list.size() <= index) {
            //Get a random number from fixed seed
            int ranNum = PickObject(i);
            //Check if this was same as previous 
            while (prev == ranNum) {
                ranNum = PickObject(++i);
            }
            prev = ranNum;
            list.add(ranNum);
            i++;
        }
        return (list.get(index));
    }
}

这可以简化,看起来我使用的循环太多了。

1 个答案:

答案 0 :(得分:1)

现在,您每次想要一个号码时都会播种RNG并拉动整个序列。你不需要这样做。相反,为rand提供更广泛的范围。这样,每次想要获得数字时都可以重复使用它。

这使事情更简单,因为您不需要每次迭代列表,因此您不必构建列表 at all 。这意味着您不再需要循环pickNumber(),而selectObject()中的大部分内容也已过时。

这个不仅更简单,而且更快。而不是每次循环/生成整个列表,你只需要抓住几个数字,直到你有一个好的数字。平均而言,这将是每次调用4/3次尝试,因为大约三分之一将是重复的。

尝试这样的事情:

public class UniqueRandomObjectSelector {
    int seed = 11231;
    int prev = -1;
    int range = 3;
    Random rand;
    int[] values;
    int maxIndex = 1000;

    public static void main(String[] args) {

       /// to generate value 37 on the fly
       System.out.println("37th value is " + selectObject(37));

        /// to print 1000 in order (and store them in an array)
        rand = new Random(seed);
        list = new int[maxIndex+1]; 
        for (int i = 0; i <= maxIndex; i++) {
            values[i] = getRandomNonRepeating();
            System.out.println(i + "th random value is " + values[i]);
        }

       /// to get value 37 from values[]
       System.out.println("37th value is " + values[37]);
    }

    static int selectObject(int index){
        rand = new Random(seed);
        int value = -1;
        for(int i=0; i<=index; i++)
            value = getRandomNonRepeating();
        return value;
    }

    static int getRandomNonRepeating() {
        int next = -1;
        while(next == prev || next < 0){
            next = rand.nextInt(range);
        }
        prev = next;
        return next;
    }
}