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,

 採用された回答

Grzegorz Knor
Grzegorz Knor 2012 年 4 月 23 日

0 投票

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

2 件のコメント

Mohammad Monfared
Mohammad Monfared 2012 年 4 月 23 日
thanks, first time to see this nice function ;)
Dmitry Kaplan
Dmitry Kaplan 2021 年 8 月 10 日
Another possibility (perhaps a little faster)
[~,idx]=sort(rand(100,1));
idx(1:70)

サインインしてコメントする。

その他の回答 (2 件)

Richard Brown
Richard Brown 2012 年 4 月 23 日

1 投票

randperm(100, 70)

2 件のコメント

Mohammad Monfared
Mohammad Monfared 2012 年 4 月 23 日
thanks, I use matlab R2011a and the randperm doesn't accept two arguments.
Richard Brown
Richard Brown 2012 年 4 月 23 日
ah yes, that came in at R2011b

サインインしてコメントする。

Jan
Jan 2012 年 4 月 23 日

1 投票

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!

Translated by