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

3 次查看(过去 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).

回答(2 个)

Steven Lord
Steven Lord 2016-4-6
Use randi.
  7 个评论
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'.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by