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

조회 수: 2 (최근 30일)
Joel Sande
Joel Sande 2016년 4월 6일
편집: Roger Stafford 2016년 4월 6일
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).

답변 (2개)

Steven Lord
Steven Lord 2016년 4월 6일
Use randi.
  댓글 수: 7
Joel Sande
Joel Sande 2016년 4월 6일
Function is not defined for 'cell' inputs.
Joel Sande
Joel Sande 2016년 4월 6일
%If I write this, it works:
conn = Connect(r);
fprintf(fid,'\n%s %s\n', num2str(Neighboor), num2str(strength));

댓글을 달려면 로그인하십시오.


Roger Stafford
Roger Stafford 2016년 4월 6일
편집: Roger Stafford 2016년 4월 6일
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
Joel Sande
Joel Sande 2016년 4월 6일
편집: Joel Sande 2016년 4월 6일
what is n ? randi for integer right ? I don't want a vector, I want only 1 number

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by