MATLAB: How to generate unique random integers between 1 to n (no two should be same)

integerrandomrandom number generator

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;

Best Answer

n = 8
l = 5
out = randperm(n,l)
OR
out1 = randperm(n)
out = out1(1:l)