How to create non-repetitive random integers

30 visualizaciones (últimos 30 días)
Mohammad Monfared
Mohammad Monfared el 23 de Abr. de 2012
Comentada: Dmitry Kaplan el 10 de Ag. de 2021
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,

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (2)

Richard Brown
Richard Brown el 23 de Abr. de 2012
randperm(100, 70)
  2 comentarios
Mohammad Monfared
Mohammad Monfared el 23 de Abr. de 2012
thanks, I use matlab R2011a and the randperm doesn't accept two arguments.
Richard Brown
Richard Brown el 23 de Abr. de 2012
ah yes, that came in at R2011b

Iniciar sesión para comentar.


Jan
Jan el 23 de Abr. de 2012
And if you are in a hurry: FEX: Shuffle

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by