randi, generate an array of unique numbers
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Marc-Olivier Labrecque-Goulet
am 23 Mär. 2017
Kommentiert: Matt J
am 23 Mär. 2017
Hi, I have an assigment where i need to generate random numbers using randi. Each number generate must be unique. Is there a fast way to do it (feature of randi function). Ex : randi(10,1,6) = [9 8 1 5 3 9] % this is not good for me because 9 is repeated.
I know I could generate the number in a loot, and verifi if it equal to an other number already written but I wonder if there is a faster way.
0 Kommentare
Akzeptierte Antwort
Matt J
am 23 Mär. 2017
Bearbeitet: Matt J
am 23 Mär. 2017
No, I don't think there is a faster way and I doubt it is meant to be fast. The direct and efficient way to do this would be to use randperm(10,6). The fact that you are asked to use randi instead shows that the exercise is being made artificially difficult, presumably to force you to work your coding muscles.
1 Kommentar
Matt J
am 23 Mär. 2017
Marc-Olivier Labrecque-Goulet commented:
well, ty anyway, I have already made a code for that but I dont like putting longer code than necessary so this is why asked.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Thermodynamics & Statistical Physics 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!