필터 지우기
필터 지우기

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;

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 2월 21일
n = 8
l = 5
out = randperm(n,l)
OR
out1 = randperm(n)
out = out1(1:l)
  댓글 수: 3
simar
simar 2012년 2월 22일
does randperm(n,l)
works in similar to
out1 = randperm(n)
out = out1(1:l)
I mean can I trust the randomness of the numbers generated by the latter method???
Karakus Zekeriya
Karakus Zekeriya 2019년 1월 19일
이동: Jan 2022년 11월 11일
Thanks Andrei; very usefull. I was tried, was gave every times true result.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by