how can i generate random number divided by 0.25 from 0 to 10 for example(0.25,7.75,5.50,6.25 ,........)?
Show older comments
for example(0.25,7.75,5.50,6.25 ,........)
Accepted Answer
More Answers (1)
% 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
Andrei Bobrov
on 18 Aug 2019
round(10*rand(11,4)/.25)*.25
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)
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)

Categories
Find more on Creating and Concatenating Matrices 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!