フィルターのクリア

How can I make randi() in a loop not choose the same number

8 ビュー (過去 30 日間)
Tariq Hammoudeh
Tariq Hammoudeh 2022 年 1 月 8 日
回答済み: Walter Roberson 2022 年 1 月 8 日
I have an array
x=zeros(1,36)
then i have inside a while loop:
while ....
y=randi(length(x));
z=randi(length(x));
How can i make it so that this doesnt choose the same number at all,
Note: the loop will end before it gets to 36 choices, so its not that it doesnt have a choice but to go over the same numbers again

採用された回答

Walter Roberson
Walter Roberson 2022 年 1 月 8 日
That would be a bit difficult. You would have to find a random number seed that you could set such that randi(36) happens to produce a permutation of the numbers 1 : 36. It could take a fair bit of work to find such a seed.
I would suggest that you take a completely different approach:
rand_y = randperm(length(x));
rand_z = randperm(length(x));
for K = 1 : length(x)
y = rand_y(K);
z = rand_z(K);
stuff
if some conditon;
break;
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by