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

 Accepted Answer

Generate 1000 of such numbers
(randi(41,1,1000)-1)/4

6 Comments

Or you could specify both the lower and upper bounds on the numbers generated by randi. This avoids the subtraction.
randi([0 40], 1, 1000)./4
If the goal is produce random numbers between 0 and N,
randi([0 N*4], 1, 1000)./4
Warning. This method is biased toward .0 values.
Run this code below many times. The distribution will always be tallest at x=0.0; Change N to 3 to see a huge bias.
N = 10;
vals = randi([0 N*4], 1, 5000000)./4;
dec = rem(vals,1); %decimal part
histogram(dec,'BinEdges',0:.25:1)
set(gca,'xtick',0:.25:1)
This is because of a slight overrepresentation of values that are a multiple of 4 and therefore will produce a .0.
For comparison, the random draw in my answer (or Andrei's simplification) is merely rounded to the nearest .25 and results in a distribution much closer to uniform.
rn = rand(1,5000000)*10;
vals = floor(rn) + round(rem(rn,1)./0.25)*.25;
dec = rem(vals,1); %decimal part
histogram(dec,'BinEdges',0:.25:1)
set(gca,'xtick',0:.25:1)
Warning. This method is biased toward .0 values.
@Adam Danz: No, your test is wrong. you test in the reminder
The request random values are in the set
0:0.25:10
There are 11 values with reminder equal to 0 and 10 values for 0.25, 0.5 and 0.75. So it is normal that when you bin by histogram the REMIDER you get a ratio of 11/10 for 0 compare to other values.
Testing on the uniform of the reminder a a wrong test for uniformity of the generated data.
If however you don't get the ratio of 11/10 on the reminder, than actually your data are not uniorm as you pretend.
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)
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)

% 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

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)

Sign in to comment.

Categories

Asked:

on 18 Aug 2019

Edited:

on 19 Aug 2019

Community Treasure Hunt

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

Start Hunting!