How to generate a random integer betwwen a and b inclusive.
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (2개)
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'.
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!