2015-10-05 77 views
0

我想在matlab中生成偏向隨機數。讓我再解釋一下,我的意思是有偏見的。 可以說我有一個定義的上限和下限分別爲30和10。 我想生成N個隨機數,偏向邊界,使得數字接近10和30(極值)的概率比它們位於中間的某些位置更多。 我該怎麼做? 任何幫助非常感謝:)在matlab中生成隨機數偏向邊界

+1

您可以使用高斯分佈(Matlab中的** randn **)和從R到[lowerbound,upperbound]的雙射。 你知道你想要獲得什麼樣的發行嗎? (即,你知道變量的分佈函數嗎?),還是你只是試圖得到「近似偏向邊界的東西」? – BillBokeey

+0

@BillBokeey嗨..感謝您的回覆。我沒有具體瞭解分配函數,只是希望它更偏向於邊界。我如何在這種情況下繼續? – Sakshi

+0

我正在發佈一個答案 – BillBokeey

回答

2
% Upper bound 
UB = 30 
% Lower bound 
LB = 0; 
% Range 
L = UB-LB; 
% Std dev - you may want to relate it to L - maybe use sigma=sqrt(L) 
sigma = L/6; 
% Number of samples to generate 
n = 1000000; 
X = sigma*randn(1,n); 
% Remove items that are above bounds - not sure if it's what you want.. if not comment the two following lines 
X(X<-L) = []; 
X(X>L) = []; 

% Take values above zero for lower bounds, other values for upper bound 
ii = X > 0; 
X(ii) = LB + X(ii); 
X(~ii) = UB + X(~ii); 

% plot histogram 
hist(X, 100); 

enter image description here

我這裏使用正態分佈但明顯可以適應使用他人..你可以改變西格瑪也。

+0

如果LB <0, UB> 0和| LB |> | UB | ? – BillBokeey

+0

正常工作與LB < 0 and UB > 0 ....當然我假設下界(LB)小於上界(UB)那裏有什麼問題? – gregswiss

+0

LB和UB值是恆定的定義值。 LB是540,UB是600 – Sakshi

1

要生成任意分佈的隨機數,您必須定義倒數cumulative distribution function。假設你叫它myICDF。一旦你獲得了這個功能,你可以使用​​生成隨機樣本。