Generating random numbers in order?

How would you generate random numbers so that they are in ascending or descending order? Is there a way to create a function similar to linspace in that it generates linearly spaced values in a certain interval but with random numbers each time you execute the function?

 Respuesta aceptada

Roger Stafford
Roger Stafford el 7 de Abr. de 2013
If a and b are the lower and upper limits you have in mind for the numbers, do this:
x = sort((b-a)*rand(n,1)+a,'ascend' or 'descend');

Más respuestas (1)

NIKHIL KUMAR
NIKHIL KUMAR el 9 de Dic. de 2021

0 votos

x = sort((10-1)*rand(n,1)+1,'ascend' or 'descend');

1 comentario

Walter Roberson
Walter Roberson el 9 de Dic. de 2021
This is the same as what Roger suggested in 2013.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

Joe
el 7 de Abr. de 2013

Comentada:

el 9 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by