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

4 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by