我想写一个函数,随后可以用一个整数参数(从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));
    }
}

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

最佳答案

现在,你正在播下RNG,每次你想要一个数字的时候,你都要拉整个序列。你不需要那么做相反,给rand一个更大的范围这样你每次想得到一个号码时都可以重复使用它。
这使事情更简单,因为您不需要每次遍历列表,所以根本不必构建列表这意味着您不再需要在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;
    }
}

07-24 14:11