MATLAB Answers

same values using randi setting seed as default

14 ビュー (過去 30 日間)
I'm using randi to generate rand integer valeus but I get the same values two times in my vector. example
rng default;
for i = 1:4
heads(i) = randi(10);
end
heads =
9
10
2
10
I wouldn't get the same values. I would get a vector where every values is different. I have to set rng default in this way I can obtain at every run of my scripts the same randi values.

  0 件のコメント

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

採用された回答

Sean de Wolski
Sean de Wolski 2012 年 9 月 21 日
See randperm if you do not want duplicate values:
randperm(10,4)

  2 件のコメント

Daniel Shub
Daniel Shub 2012 年 9 月 21 日
I thought randperm took a second argument, but in R2011a it doesn't.
Sean de Wolski
Sean de Wolski 2012 年 9 月 21 日
The second argument was added in 11b I believe when randperm was rewritten as builtin.

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

その他の回答 (4 件)

Wayne King
Wayne King 2012 年 9 月 21 日
編集済み: Wayne King 2012 年 9 月 21 日
I'm not sure exactly what you're asking here. If you are calling
randi(10)
even a small number of times (here 4 times), then I would expect that you would get repeated values with a substantial probability. There are only ten values to choose from.
With respect to:
I wouldn't get the same values. I would get a vector where every values is different. I have to set rng default in this way I can obtain at every run of my scripts the same randi values.
If you use rng with a seed, then you will obtain the same values every time. Are you asking for a seed where you will get 4 different values?
For example:
rng(100)
x = randi(10,4,1);
Also, why are you going randi() in a for loop like that, you can just create a vector and be done with it.
To recreate your example:
rng default;
x = randi(10,4,1);

  2 件のコメント

Salvatore Mazzarino
Salvatore Mazzarino 2012 年 9 月 21 日
Yes I would a seed that produce values different. I wouldn't get a vector with same values as the second and the forth.
Wayne King
Wayne King 2012 年 9 月 21 日
then if you want some randomness, don't seed and call randi(), use Sean's suggestion below.

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


Peter Perkins
Peter Perkins 2012 年 9 月 21 日
As others have said, randi is for "sampling with replacement", while randperm is for "sampling without replacement". If you happen to have the Statistics Toolbox, you will also find the datasample function (a newer replacement for randsample) that provides both kinds of sampling, as well as weighted versions of both.
You code snippet showed a loop with repeated calls to randi. If you need to be able to sample without replacement from 1:n in repeated calls, you'll need to maintain some kind of record of what values have already been drawn. That's not hard to do, but the magic of nested functions allows you to do this in a very nice way:
function s = makeWORSampler(n)
last = 0;
values = randperm(n);
s = @worSampler;
function x = worSampler(k)
if last+k > n
error('Not enough values remaining to be sampled.');
end
x = values(last+(1:k));
last = last + k;
end
end
>> s = makeWORSampler(10)
s =
@makeWORSampler/worSampler
>> s(5)
ans =
8 9 2 10 3
>> s(4)
ans =
7 4 1 5
>> s(1)
ans =
6
>> s(1)
Error using makeWORSampler/worSampler (line 7)
Not enough values remaining to be sampled.
Hope this helps.

  0 件のコメント

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


Daniel Shub
Daniel Shub 2012 年 9 月 21 日
Do you want something like
temp = randperm(10);
heads = temp(1:4)
In this way heads will always have 4 values. These values will be chosen without replacement from the integers between 1 and 10 inclusive.

  0 件のコメント

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


Javier
Javier 2012 年 9 月 21 日
編集済み: Javier 2012 年 9 月 21 日
Go to the help of Matlab (mine M2012Ra) and search for "Updating Your Random Number Generator Syntax"
for i = 1:4
rng('shuffle');
heads(i) = randi(10);
end
Consider also Mr King answer.
Javier

  0 件のコメント

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

Community Treasure Hunt

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

Start Hunting!

Translated by