Effacer les filtres
Effacer les filtres

Generating random numbers in order?

14 vues (au cours des 30 derniers jours)
Joe
Joe le 7 Avr 2013
Commenté : Walter Roberson le 9 Déc 2021
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?

Réponse acceptée

Roger Stafford
Roger Stafford le 7 Avr 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');
  1 commentaire
Joe
Joe le 7 Avr 2013
Thanks a bunch!

Connectez-vous pour commenter.

Plus de réponses (1)

NIKHIL KUMAR
NIKHIL KUMAR le 9 Déc 2021
x = sort((10-1)*rand(n,1)+1,'ascend' or 'descend');
  1 commentaire
Walter Roberson
Walter Roberson le 9 Déc 2021
This is the same as what Roger suggested in 2013.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by