How to generate a matrix with random numbers between 0 and 1 and mean of the distribution is unspecified.

2 ビュー (過去 30 日間)
Every time I generate matrix with random numbers between 0 and 1, mean of the generated distribution is different.
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 6 月 3 日
Are you asking how to deliberately generate a different mean each time? If so then I suggest generate random coefficients for a beta distribution.

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

回答 (2 件)

Torsten
Torsten 2022 年 6 月 3 日
If you choose the number of random numbers large enough, their mean should approach 0.5:
n = 20000;
y = rand(n,1);
ym = mean(y)
If you want the same numbers ( and thus the same mean ) in each run, use
rng('default')

John D'Errico
John D'Errico 2022 年 6 月 3 日
You need to understand the difference between a sample mean, and the population mean.
x = rand(1,100);
mean(x)
ans = 0.4463
This is a random SAMPLE, so at best, it will only have a mean near the population mean for that uniform random number generator, which is theoretically 0.5 exactly.
With larger sample size, the sample mean approaches the population mean, but that is only an asymptotic thing.
mean(rand(1,1e6))
ans = 0.4999
If you really want a sample with the sample mean EXACTLY the same as the population mean, then your sample is not as easy to generate. However, it can be done using Roger Stafford's randfixedsum, as found on the file exchange. Better here is probably to just understand the difference.

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by