How to generate unique random integers between 1 to n (no two should be same)
91 次查看(过去 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!