How to generate unique random integers between 1 to n (no two should be same)
29 ビュー (過去 30 日間)
古いコメントを表示
Hello I want to generate (<n) unique random integers varying from 1 to n. I used the existing randint function which seems to be producing identical numbers between.
This is the line of code that I'm using
k = randint(l,1,(a(1)*a(2))-1)+1;
0 件のコメント
採用された回答
Andrei Bobrov
2012 年 2 月 21 日
n = 8
l = 5
out = randperm(n,l)
OR
out1 = randperm(n)
out = out1(1:l)
3 件のコメント
Karakus Zekeriya
2019 年 1 月 19 日
移動済み: Jan
2022 年 11 月 11 日
Thanks Andrei; very usefull. I was tried, was gave every times true result.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!