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 views (last 30 days)
for example(0.25,7.75,5.50,6.25 ,........)

Accepted Answer

Bruno Luong
Bruno Luong on 18 Aug 2019
Edited: Bruno Luong on 18 Aug 2019
Generate 1000 of such numbers
(randi(41,1,1000)-1)/4
  6 Comments
Adam Danz
Adam Danz on 19 Aug 2019
Edited: Adam Danz on 19 Aug 2019
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 on 19 Aug 2019
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.

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 18 Aug 2019
Edited: Adam Danz on 18 Aug 2019
% 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 Comments
Bruno Luong
Bruno Luong on 19 Aug 2019
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 on 19 Aug 2019
Edited: Adam Danz on 19 Aug 2019
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)

Sign in to comment.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!