简体   繁体   English

选择均匀随机的站点以阻止剩余的阵列站点

[英]Choosing uniformly random sites to block from remaining array sites

I have an appication of a grid where the two dimensional array represents sites which may be blocked or open , take blocked as 0 and open as 1 . 我有一个网格的应用,其中二维数组表示可能被阻止或打开的站点,将被阻止的设置为0 ,将打开的设置为1 Let the boolean array be boolean gridSite[N*N] . 令布尔数组为boolean gridSite[N*N] I am using a one dimensional array for two dimensions by correctly changing the index from the user into required index in the 1-D array . 通过将用户的索引正确更改为一维数组中的所需索引,我将一维数组用于二维。 The function which changes the index is as follows int index(int i,int j){ return (i-1)*N + j-1 ;} . 更改索引的函数如下int index(int i,int j){ return (i-1)*N + j-1 ;} The user gives an index b/w 1 and N for both dimension . 用户给出两个维度的索引b / w 1N

During a simulation I require to open the sites by randomly choosing them . 在模拟过程中,我需要通过随机选择来打开站点。 I mean initially all the sites are blocked . 我的意思是最初所有站点都被封锁了。 Next step is to choose random index i and j and open it . 下一步是选择随机索引ij并将其打开。 Repeat this again (randomly) till when the condition is met . 再次(随机)重复此操作,直到满足条件为止。 I have a library for specific purpose which can generate random number by this code StdRandom.uniform(N) but the point is , this doesn't solve the problem . 我有一个特定用途的库,可以通过此代码StdRandom.uniform(N)生成随机数,但要点是,这不能解决问题。 When run for the first time it will generate a random site and open it , but now I have to generate a random index only for the remaining locations . 第一次运行时,它将生成一个随机站点并打开它,但是现在我只需要为其余位置生成一个随机索引。 Suppose we have 20x20 grid and for the first time it chooses 4,7 then next time it shouldn't choose this anymore . 假设我们有20x20的网格,并第一次选择了4,7然后下次不再选择它。 How to achieve this 如何做到这一点

It sounds like you need a random permutation of numbers from 1 to N , where N = 20 * 20 = 400 in this case. 听起来您需要从1到N的数字随机排列,在这种情况下, N = 20 * 20 = 400

  1. Write a loop to generate the numbers between 1 and N in an ordered list or array. 编写循环以在有序列表或数组中生成介于1和N之间的数字。
  2. Use Java's standard libraries to shuffle this list and create a random permutation. 使用Java的标准库来随机排列此列表并创建随机排列。
  3. Iterate through the permuted list, and transform the indices using your function to map the single index to a paired index. 遍历排列的列表,并使用您的函数将索引转换为将单个索引映射到成对的索引。

Use the following class which will sequentially give you a random index and enumerate all your indices exactly once. 使用以下类,该类将顺序为您提供一个随机索引,并一次枚举所有索引。

import java.util.Enumeration;
import java.util.Random;

public class RandomPermuteIterator implements Enumeration<Long> {
    int c = 1013904223, a = 1664525;
    long seed, N, m, next;
    boolean hasNext = true;

    public RandomPermuteIterator(long N) throws Exception {
        if (N <= 0 || N > Math.pow(2, 62)) throw new Exception("Unsupported size: " + N);
        this.N = N;
        m = (long) Math.pow(2, Math.ceil(Math.log(N) / Math.log(2)));
        next = seed = new Random().nextInt((int) Math.min(N, Integer.MAX_VALUE));
    }

    public static void main(String[] args) throws Exception {
        RandomPermuteIterator r = new RandomPermuteIterator(100);
        while (r.hasMoreElements()) System.out.print(r.nextElement() + " ");
        //output: 50 52 3 6 45 40 26 49 92 11 80 2 4 19 86 61 65 44 27 62 ...
    }

    @Override
    public boolean hasMoreElements() {
        return hasNext;
    }

    @Override
    public Long nextElement() {
        next = (a * next + c) % m;
        while (next >= N) next = (a * next + c) % m;
        if (next == seed) hasNext = false;
        return  next;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM