how to generate random integer number in a fixed range in MATLAB, like between 1 to 10
Show older comments
i want to generate random number between 1 to 10 answer like: 7 4 1 8 5 2 10 6 9 3
1 Comment
Siddharth Gupta
on 7 Nov 2019
x=randi([1,10],1,10);
Accepted Answer
More Answers (2)
Jan
on 10 Jan 2013
If repetitions are not wanted:
c = randperm(10);
7 Comments
Shamsuddeen Abdullahi
on 10 Apr 2019
Please, what if I want repeatitions?
Yifu Luo
on 5 Jul 2019
randi([min max], [size_x, size_y])
Priodyuti Pradhan
on 28 Oct 2020
It is perfectly working! Thank you for sharing!
GAGANDEEP KAUR
on 12 Jan 2021
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?
Walter Roberson
on 12 Jan 2021
We would need to see your code, GAGANDEEP KAUR
Nitish Jha
on 16 Mar 2021
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.
Ruochen
on 26 Sep 2019
4 votes
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.
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!