how can i generate random number divided by 0.25 from 0 to 10 for example(0.​25,7.75,5.​50,6.25 ,........)?

4 ビュー (過去 30 日間)
for example(0.25,7.75,5.50,6.25 ,........)

採用された回答

Bruno Luong
Bruno Luong 2019 年 8 月 18 日
編集済み: Bruno Luong 2019 年 8 月 18 日
Generate 1000 of such numbers
(randi(41,1,1000)-1)/4
  6 件のコメント
Adam Danz
Adam Danz 2019 年 8 月 19 日
編集済み: Adam Danz 2019 年 8 月 19 日
I interpret the question as a need to draw random numbers rounded to the nearest quarter and bounded by [0,10]. Under that interpretation, we would expect a uniform distribution of numbers ending in .00, .25, .50. 75. A non-uniform distribution would be a sign that draws of .00, .25, .50, .75 were not truly random.
However, I can also see your interpretation (and the OP's, apparently) that the random draw should include equal representation of the bounds which, since they are integers, would result in a greater number of draws from the x.00 category.
The warning is important under the first interpretation above. But your answer is definitely more appropriate for the 2nd interpretation. (+1)
Bruno Luong
Bruno Luong 2019 年 8 月 19 日
In probability the two are defined ususually as following in the textbook
  1. Y is uniform distribution on the set { 0, 0.25, 0.5, ... 10 } (mine);
  2. Conditional distribution X = uniform distribution on [0,10], Y = { X | X is divisible by 0.25 } (yours).
Usually if the person does not specify, or in the case here the interval [0,10] is speficied after the divisibility requirement, as seems to indicated the subject of the thread, then I assume 1.

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

その他の回答 (1 件)

Adam Danz
Adam Danz 2019 年 8 月 18 日
編集済み: Adam Danz 2019 年 8 月 18 日
% Generate 12 random number between [0,10]
rn = rand(1,12)*10;
% Round to nearest .25
rnRounded = floor(rn) + round(rem(rn,1)./0.25)*.25;
  3 件のコメント
Bruno Luong
Bruno Luong 2019 年 8 月 19 日
WARNING: Round method will produce variables with two extreme values appear twice-less than interior values
r=round(10*rand(1,1e6));
histogram(r)
Adam Danz
Adam Danz 2019 年 8 月 19 日
編集済み: Adam Danz 2019 年 8 月 19 日
As Bruno mentions, this answer will have a smaller probability of selecting the bounds [0,10] which can be demonstrated by the following (subplot 1).
x = round(10*rand(1,500000)/.25)*.25;
histogram(x, 'BinEdges',0:.25:10.25)
However, if the goal is to have a uniform distribution of values that end in [.00 .25, .50. 75] bounded by [0,10], this solution will accomplish that (subplot 2).
x = round(10*rand(1,500000)/.25)*.25;
dec = rem(x,1); %decimal part
histogram(dec,'BinEdges',0:.25:1)
set(gca,'xtick',0:.25:1)

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

カテゴリ

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