How to create non-repetitive random integers

34 views (last 30 days)
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,

Accepted Answer

Grzegorz Knor
Grzegorz Knor on 23 Apr 2012
Try this code:
N = 100;
x = randperm(N);
x = x(1:70)
  2 Comments
Mohammad Monfared
Mohammad Monfared on 23 Apr 2012
thanks, first time to see this nice function ;)
Dmitry Kaplan
Dmitry Kaplan on 10 Aug 2021
Another possibility (perhaps a little faster)
[~,idx]=sort(rand(100,1));
idx(1:70)

Sign in to comment.

More Answers (2)

Richard Brown
Richard Brown on 23 Apr 2012
randperm(100, 70)
  2 Comments
Mohammad Monfared
Mohammad Monfared on 23 Apr 2012
thanks, I use matlab R2011a and the randperm doesn't accept two arguments.

Sign in to comment.


Jan
Jan on 23 Apr 2012
And if you are in a hurry: FEX: Shuffle

Community Treasure Hunt

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

Start Hunting!