Problem with creating a random permutation with seed

Hello,
I need to create a random permutation of numbers. I also need to be able to create the same random permutation several times (for tests).
I have read in the documentation that this can be achieved either with "rng(Seed)" or by setting RandStream. I tried both in the matlab command line and they both don't work for me.
I get an eror message "??? Undefined function or method 'rng' for input arguments of type 'double'." for the example "rng(54321)".
And I get an error message "??? The class RandStream has no property or method named 'setGlobalStream'." while trying out the example "s = RandStream('mt19937ar','Seed',0) RandStream.setGlobalStream(s);". When I simply try "rand(s,10,1)" after defining s I get different results each time. The matlab version I use is R2010a.
Can you give me hint how to solve this problem, please?
Alexandra

댓글 수: 4

Which documentation are you reading? Are you reading the one in the product (that came with your installation)? "rng" and "setGlobalStreams" aren't available in your version. It shouldn't even mention it in your documentation.
Try reading your documentation in the product. Also, try searching for "setDefaultStream".
You are right, apparently "rng" and "setGlobalStreams" aren't available in my version.
If I define s = RandStream('mcg16807', 'Seed',0) and give it as an argument to "randperm", shouldn't it be doing the same job? I run "randperm" after that and still get different permutations each time. I also tried "setDefaultStream", it doesn't give the required result either.
Is there another possibility to control the generation of a random permutation I am not aware of?
MOLLAH KHOKON ALI
MOLLAH KHOKON ALI 2017년 10월 7일
편집: MOLLAH KHOKON ALI 2017년 10월 7일
Hello,
I am also facing this problem and I am using Matlab R2010a version so What kind of work can I do for this solution.
Please Give me information very quickly.
Mollah Khokon Ali
For those old versions,
random('seed', VALUE)
if I recall correctly.

댓글을 달려면 로그인하십시오.

답변 (3개)

Image Analyst
Image Analyst 2012년 2월 19일

0 개 추천

rand() does not give random permutations - it gives random numbers. If, as you say, you need random permutations, you need to use randperm().

댓글 수: 4

I am using randperm() already, but it gives me different permutations each time. The documentation mentions the possibility to give RandStream as an argument to randperm, but it doesn't give me required result.
Seems to work fine for me:
for k = 1:10
% Seed with the same seed every time.
rng(10);
% Generate the same permutation every time.
randperm(15)
end
What did you do????
Image Analyst, same here - works for me.
However, if I run rng(10) followed by multiple randperm's then I get different results every time.
That's expected.
rng(10); % "Shuffle" a "deck of cards"
theHands = zeros(4, 5);
theHands(1, :) = randperm(52, 5);
theHands(2, :) = randperm(52, 5);
theHands(3, :) = randperm(52, 5);
theHands(4, :) = randperm(52, 5)
Each of those hands is different. [Though with this approach the same card can come up in multiple hands (and does.) For this application you'd probably want to draw 20 cards at once with randperm(52, 4*5) and reshape it afterwards into four 5-card hands to avoid duplicating cards.]
If you "shuffle the deck" once and "remember" exactly how you shuffled it, you can generate the same hands by shuffling it the same way again. For most people, this would be difficult; for stage magicians and for MATLAB, it's easier.
rng(10); % Shuffle
hand1 = randperm(52, 5)
rng(10); % Stack the deck back the same way as before drawing hand1
hand2 = randperm(52, 5)
Both those hands are the same.
You may be tempted to reseed the generator every time you need random numbers. Resist that temptation; see the Note on this documentation page for some explanation.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2012년 2월 19일
for k = 1:10
% Seed with the same seed every time.
rng(10);
% Generate the same permutation every time.
randperm(15)
end

댓글 수: 1

I doesn't work for me unfortunately, because apparently my version does not allow the use of "rng".

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2012년 2월 19일

0 개 추천

I see no evidence in what you have written that you are resetting the seed each time. Setting the default stream does NOT reset the seed.

댓글 수: 1

I didn't realize I had to set default sream every time before using it. Thanks, now it works the way I wanted!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2012년 2월 19일

댓글:

2019년 9월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by