Is it possible to generate specific random numbers?

1 回表示 (過去 30 日間)
Omar Ali Muhammed
Omar Ali Muhammed 2021 年 5 月 2 日
コメント済み: Steven Lord 2021 年 5 月 3 日
The generation of unique random numbers is done simply through randperm.
example
function idx=RandNumberSequence(Key,N)
% using Key as seed to the used random number generator.
Key = max(cumsum(double(Key))) ;
s=rng(Key); % Set the random number generator seed
idx=randperm(N);
end
The question is, given certain N random numbers, is it possible to select the Key that generates these N numbers?
let N=10, let te random numbers be 89, 70, 10, 12, 67, 25, 200, 18, 90, 86
Key=? (to be selected)

採用された回答

Steven Lord
Steven Lord 2021 年 5 月 2 日
Sure.
seedvalue = 0;
expected = [89 70 10 12 67 25 200 18 90 86];
rng(seedvalue, 'twister');
actual = randperm(max(expected), numel(expected));
while ~isequal(actual, expected)
seedvalue = seedvalue + 1;
rng(seedvalue, 'twister');
actual = randperm(max(expected), numel(expected));
end
It may take a while. This assumes you haven't manually created a state vector (which would be an advanced maneuver.)
If you're asking if there's a clever way to directly go from the sequence to the seed value, I'm not aware of any offhand.
If you know you're going to need or want to reproduce a given sequence, record the seed before generating the sequence.
  2 件のコメント
Omar Ali Muhammed
Omar Ali Muhammed 2021 年 5 月 3 日
However, Speed is the main challenge espicially for long sequence. Is it possible to parallelize the code?
Steven Lord
Steven Lord 2021 年 5 月 3 日
Yes, this code could be parallelized through the use of parfor to sweep the seed space. But if speed is of the essence the best solution is to record the seed before you generate the sequence rather than trying to determine the seed after the fact.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by