Random number in a specific range

Hello,,I wanna generate random numbers from range for example [-0.2,0.2] using matlab functions like randn or sth. thanks in advance.

 採用された回答

bym
bym 2012 年 5 月 13 日

2 投票

x = .4 * rand(100,1)-.2;

1 件のコメント

Walter Roberson
Walter Roberson 2012 年 5 月 14 日
This answer is good if you want uniform distribution. If you want normal distribution then this answer will not work; see my Answer.

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

その他の回答 (2 件)

Oleg Komarov
Oleg Komarov 2012 年 5 月 13 日

0 投票

You can scroll down the documentation of randn for an example.
EDIT You don't even need to scroll down.

2 件のコメント

Walter Roberson
Walter Roberson 2012 年 5 月 14 日
The closest I see in the randn documentation is for a given mean and standard deviation. Nothing about a given range. Not for randn.
http://www.mathworks.com/help/techdoc/ref/randn.html
Oleg Komarov
Oleg Komarov 2012 年 5 月 14 日
Right, on the FEX there are contributions with random generators from truncated normal.

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

Walter Roberson
Walter Roberson 2012 年 5 月 14 日

0 投票

It is not possible to construct a normal distribution over a restricted range. By definition a normal distribution must have an infinite tail in both directions.
If you need something that is much like a normal distribution but is defined over a limited range, then consider using a beta distribution.

カテゴリ

ヘルプ センター および File ExchangeRandom Number Generation についてさらに検索

製品

質問済み:

2012 年 5 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by