How to generate unique random integers between 1 to n (no two should be same)
91 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 21 Feb. 2012
n = 8
l = 5
out = randperm(n,l)
OR
out1 = randperm(n)
out = out1(1:l)
3 Kommentare
Karakus Zekeriya
am 19 Jan. 2019
Verschoben: Jan
am 11 Nov. 2022
Thanks Andrei; very usefull. I was tried, was gave every times true result.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Random Number Generation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!