uniform distribution between a and b with intervals of 0,005

1 回表示 (過去 30 日間)
Alexandra
Alexandra 2016 年 11 月 25 日
コメント済み: Alexandra 2016 年 11 月 28 日
Hi,
I am having trouble using R = unidrnd(N) to create n random numbers between a and b (imagine a=0 and b=0.2) where the numbers generated are always a multiple of 0,005. For example: 0,005 0,1 0,15 0,0155 ...
Thanks a lot,
  1 件のコメント
Alexandra
Alexandra 2016 年 11 月 25 日
Thanks a lot for the help. I will use tic toc to check which way is faster as the model is very heavy.

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

採用された回答

Guillaume
Guillaume 2016 年 11 月 25 日
How about generating uniform integers between 0 and 0.2/0.005 and multiplying the whole lot by 0.005
R = randi([0 0.2/0.005], 1, 1000) * 0.005; %generate 1000 numbers in multiple of 0.005 between 0 and 0.2

その他の回答 (2 件)

Image Analyst
Image Analyst 2016 年 11 月 25 日
Alexandra, try this:
numValues = 20; % However many elements you want.
a=0.1;
b = 0.2;
% Get max integer value for randi.
topValue = floor((b-a)/0.005)
% Scale to make values go from a to b.
R = a + 0.005 * randi(topValue, 1, numValues)
  1 件のコメント
Alexandra
Alexandra 2016 年 11 月 28 日
This solution is close to uniform but never takes value zero (a), which is important.

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


dpb
dpb 2016 年 11 月 25 日
Well, I'd guess so...that wouldn't be very random at all...but, the simple-minded approximation would be
>> N=10;
>> n=rand(N,1)*0.2;
>> n=(n*1000-mod(n*1000,5))/1000
n =
0.0950
0.1600
0.0250
0.0800
0.1800
0.1550
0.1900
0.1300
0.0050
0.1650
>>
  1 件のコメント
Alexandra
Alexandra 2016 年 11 月 28 日
Hi, sorry, with b=0,045 the max number generated was 0,04.

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

Community Treasure Hunt

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

Start Hunting!

Translated by