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

조회 수: 5 (최근 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 CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by