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!