cuốn sách gpt4 ai đã làm

matlab - Matlab中并行循环中的不同(伪)随机数

In lại Tác giả: Vũ trụ không gian 更新时间:2023-11-03 20:29:55 30 4
mua khóa gpt4 Nike

hiện hữu并行循环中请求随机数总是返回相同的伪随机数。我怎样才能避免这种情况?

% workers initialization:
if matlabpool('size') == 0
matlabpool('open',2);
khác
matlabpool('close');
matlabpool('open',2);
end

% parallel loop always give the same random numbers...
parfor k = 1:10
fprintf([num2str(rand(1,1)), ' ']);
end

一个理想的解决方案是通过 CPU 时间或类似方式在每个线程中初始化伪随机数生成器。 rng('shuffle') 之类的东西在这里似乎没有帮助...

Đầu ra của bảng điều khiển:

Sending a stop signal to all the workers ... stopped.
Starting matlabpool using the 'local' profile ... connected to 2 workers.
0.32457 0.66182 0.63488 0.64968
0.26459 0.096779 0.50518 0.48662 0.034895 0.85227

câu trả lời hay nhất

documentation here关于这里的各种选择。这是您可以做一些接近的事情的一种方法。

numWorkers = matlabpool('size');
[streams{1:numWorkers}] = RandStream.create('mrg32k3a', ...
'Seed', 'shuffle', 'NumStreams', numWorkers);
spmd
RandStream.setGlobalStream(streams{labindex});
end

或者,为了避免在客户端创建所有流,您可以改为这样做:

rng('shuffle'); % shuffle the client
workerSeed = randi([0, 2^32-1]);
spmd
stream = RandStream.create('mrg32k3a', ...
'Seed', workerSeed, ...
'NumStreams', numlabs, ...
'StreamIndices', labindex);
RandStream.setGlobalStream(stream);
end

关于matlab - Matlab中并行循环中的不同(伪)随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21604008/

30 4 0
không gian vũ trụ
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress