How to create non-repetitive random integers

Hi,
As the title suggests I want to create non-repetitive random integers but don't know how to... say 70 integers ranging from 1 to 100. is there a straight way to do this?
thanks,

 Akzeptierte Antwort

Grzegorz Knor
Grzegorz Knor am 23 Apr. 2012

0 Stimmen

Try this code:
N = 100;
x = randperm(N);
x = x(1:70)

2 Kommentare

thanks, first time to see this nice function ;)
Another possibility (perhaps a little faster)
[~,idx]=sort(rand(100,1));
idx(1:70)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Richard Brown
Richard Brown am 23 Apr. 2012

1 Stimme

randperm(100, 70)

2 Kommentare

thanks, I use matlab R2011a and the randperm doesn't accept two arguments.
Richard Brown
Richard Brown am 23 Apr. 2012
ah yes, that came in at R2011b

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by