# How to generate a random integer betwwen a and b inclusive.

3 views (last 30 days)
Joel Sande on 6 Apr 2016
Edited: Roger Stafford on 6 Apr 2016
Good morning, I would like to know How to generate a random integer betwwen 1 and 10 inclusive. the integer should be 1 or .... or 10
Thanks (I ve already done it before, but I lost the code).
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Answers (2)

Steven Lord on 6 Apr 2016
Use randi.
##### 7 CommentsShow 5 older commentsHide 5 older comments
Joel Sande on 6 Apr 2016
Function is not defined for 'cell' inputs.
Joel Sande on 6 Apr 2016
%If I write this, it works:
conn = Connect(r);
fprintf(fid,'\n%s %s\n', num2str(Neighboor), num2str(strength));

Sign in to comment.

Roger Stafford on 6 Apr 2016
Edited: Roger Stafford on 6 Apr 2016
x = 9*rand(n,1)+1;
As to "inclusive" this will never give exactly 1 or exactly 10. However if you intended to have an uniform distribution between 1 and 10 the theoretical probability of either extreme is appropriately zero.
Addendum: Ignore this solution. I didn't notice the word 'integer'.
##### 1 CommentShow -1 older commentsHide -1 older comments
Joel Sande on 6 Apr 2016
Edited: Joel Sande on 6 Apr 2016
what is n ? randi for integer right ? I don't want a vector, I want only 1 number

Sign in to comment.

### Categories

Find more on Creating and Concatenating Matrices 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!