How to generate a random number between 0 and 180

6 ビュー (過去 30 日間)
Victoria Helm
Victoria Helm 2021 年 7 月 8 日
編集済み: DGM 2021 年 7 月 12 日
Will the following code work to generate a random integer between 0 and 180?
length = 180;
random_location = randi([0 length], 1);

回答 (2 件)

Image Analyst
Image Analyst 2021 年 7 月 12 日
Don't use length as a variable name because it's the name of a built-in function. Try it this way:
% Generate a single integer, randomly chosen from the range 0 to 180, inclusive.
maxValue = 180; % Not using length because it's a built-in function.
randomInteger = randi([0, maxValue], 1, 1) % Generate the column vector.

DGM
DGM 2021 年 7 月 8 日
編集済み: DGM 2021 年 7 月 8 日
Yes.
length = 180;
random_location = randi([0 length],1E6,1);
[min(random_location) max(random_location)]
ans = 1×2
0 180
histogram(random_location,'binmethod','integers')
  2 件のコメント
Victoria Helm
Victoria Helm 2021 年 7 月 12 日
What is the purpose of including the 1E6?
DGM
DGM 2021 年 7 月 12 日
編集済み: DGM 2021 年 7 月 12 日
That's the height of the array; i'm specifying that I want a 1000000x1 column vector from randi(). The reason I chose to get a large vector instead of 1 number is so that I had enough samples to demonstrate (via histogram) that the results are uniformly distributed between the specified limits. You can specify whatever output size you need.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by