【问题标题】:Sequence of random selection with no two consecutive objects same没有两个连续对象相同的随机选择序列
【发布时间】:2013-11-29 11:09:34
【问题描述】:

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

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

【问题讨论】:

  • 有一个快速简单的算法:生成二进制数字序列。各加一个。计算它们的模 3 的累积和。这不应该超过一两行代码,具体取决于口味,并且通过避免测试和消除,它可能会很快。

标签: java algorithm random random-seed


【解决方案1】:

现在,您正在播种 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;
    }
}

【讨论】:

  • 这确实很干净,但是我每次都必须重新生成整个序列的唯一原因是因为我需要一个主函数,它只需要序列号并给我与之关联的随机数。我的最终目标是调用 selectObject(int index) 作为 web 服务的一部分,它将索引作为查询参数。我的程序将没有像上面这样的 main 函数来同时生成所有数字。
  • 啊,我明白了。好吧,您仍然可以更有效地做到这一点。基本上你的selectObject(index) 将像上面的main() 一样循环,但返回最后一个而不是全部打印出来。让我更新一下代码示例。
  • 或者,如果您知道index 不会超过某个值,您可以将它们(最多)预先计算到一个数组中并永久存储它。然后就像每次检索一个数字一样简单。
  • 您建议的替代解决方案的问题是,此函数位于大规模 Web 服务的端点,允许我进行任意数量的计算,但严格来说没有持久性或缓存。
  • 在你的新代码中,我们将变量 prev 全局存储,我们可以将它带入任何方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-24
  • 2016-04-18
  • 2019-03-18
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
相关资源
最近更新 更多