how to generate random integer number in a fixed range in MATLAB, like between 1 to 10

 Réponse acceptée

doc randi
and simple example:
randi([1 10],40)

1 commentaire

If repititions are not required:
c = randperm(20);
disp(c)
5 15 18 17 1 19 14 16 10 20 12 9 11 2 3 4 13 7 8 6

Connectez-vous pour commenter.

Plus de réponses (2)

If repetitions are not wanted:
c = randperm(10);

7 commentaires

Please, what if I want repeatitions?
It is perfectly working! Thank you for sharing!
I was tryingh to fit some variables using nlinfit for which i have generated random integers with randi but after fitting it has chosen some values as decimals numbers. If I can get any specific reason for the same?
Hii...Prioduyti and others...How to use this code with a component used in simulink .I want to use randi function with a component of matlab simulink (lead screw)..how to link this function with that....Please guide.

Connectez-vous pour commenter.

Use randsample
if the range is 1 to n:
y = randsample(n,k) returns k values sampled uniformly at random, without replacement, from the integers 1 to n.
if the range is say 8 to 23, choose 6 randon mumbers
population = 8:23;
y = randsample(population,6)
y = randsample(population,k) returns a vector of k values sampled uniformly at random, without replacement, from the values in the vector population.

Catégories

En savoir plus sur Variables dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by