Effacer les filtres
Effacer les filtres

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

91 vues (au cours des 30 derniers jours)
simar
simar le 21 Fév 2012
Déplacé(e) : Jan le 11 Nov 2022
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;

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 21 Fév 2012
n = 8
l = 5
out = randperm(n,l)
OR
out1 = randperm(n)
out = out1(1:l)
  3 commentaires
simar
simar le 22 Fév 2012
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 le 19 Jan 2019
Déplacé(e) : Jan le 11 Nov 2022
Thanks Andrei; very usefull. I was tried, was gave every times true result.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by