3

我需要进行1000计算,每一个都基于一个随机数。整个运行需要可重现

在单线程环境中,我只是根据种子创建随机数并将其用于每次计算:

Random random = new Random(37);
for (Calculation c : calculations) {
    c.doCalculation(r.nextInt());
}

在多线程环境中,我有10线程,我将有一个种子随机种子线程 Random 的:

Random initRandom = new Random(37);
for (List<Calculation> p : calculationPartitions) {
    final Random threadRandom = new Random(initRandom.nextLong());
    executorService.submit(() -> {
        for (Calculation c : p) {
            c.doCalculation(threadRandom.nextInt());
        }
    });
}
// Merge the calculation results back in the same order as the calculationPartitions
...

这是一个好主意吗?一般来说,它仍然是均匀分布的随机数吗?threadRandom每个都是由 播种的事实,initRandom它不会破坏均匀分布吗?

出于可重现的原因,我不能共享 1 个全局随机数,因为某些线程在某些运行中可能比其他线程运行得更快,因此它们不会总是以相同的顺序调用全局随机数(并且争用可能会导致性能下降)。

4

2 回答 2

1

特别是关于均匀分布,每 10 个像素有 1 个随机位置似乎没问题:

分配

哦等等,每 1 个像素有 1 个随机位置,种子线程方法似乎更好!?在完美的均匀分布中(对于大小 250000),它将是全黑的:

在此处输入图像描述

剩下:

public class SingleRandomProof extends JFrame {

    public static final int WIDTH = 500;
    public static final int HEIGHT = 500;
    public static final int SIZE = WIDTH * HEIGHT;

    public static void main(String[] args) {
        SingleRandomProof proof = new SingleRandomProof();
        proof.pack();
        proof.setVisible(true);
        proof.doCalc();
    }

    private JLabel panel;

    public SingleRandomProof() throws HeadlessException {
        super("1 random");
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
        panel = new JLabel(new ImageIcon(image));
        setContentPane(panel);
    }

    private void doCalc() {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.setColor(Color.BLACK);
        Random r = new Random(37);
        for (int i = 0; i < SIZE; i++) {
            int position = r.nextInt(SIZE);
            g.fillRect(position % HEIGHT, position / HEIGHT, 1, 1);
        }
        panel.setIcon(new ImageIcon(image));
    }

}

正确的:

public class SeededThreadRandomProof extends JFrame {

    public static final int WIDTH = 500;
    public static final int HEIGHT = 500;
    public static final int SIZE = WIDTH * HEIGHT;

    public static void main(String[] args) {
        SeededThreadRandomProof proof = new SeededThreadRandomProof();
        proof.pack();
        proof.setVisible(true);
        proof.doCalc();
    }

    private JLabel panel;

    public SeededThreadRandomProof() throws HeadlessException {
        super("10 seeded randoms");
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
        panel = new JLabel(new ImageIcon(image));
        setContentPane(panel);
    }

    private void doCalc() {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.setColor(Color.BLACK);
        Random initRandom = new Random(37);
        for (int j = 0; j < 10; j++) {
            Random r = new Random(initRandom.nextLong());
            for (int i = 0; i < SIZE / 10; i++) {
                int position = r.nextInt(SIZE);
                g.fillRect(position % HEIGHT, position / HEIGHT, 1, 1);
            }
        }
        panel.setIcon(new ImageIcon(image));
    }

}
于 2016-09-01T13:47:07.327 回答
1

您将通过 Random 获得数字的均匀分布,但是不能保证列表中每个 Calculation 对象列表的运行顺序,因此如果这些然后将结果传递给共享列表,则顺序可能会随着运行而变化。

于 2016-08-31T15:51:29.537 回答