MATLAB generate random numbers

陌路散爱 提交于 2019-11-28 02:28:13

Perhaps you want to generate normally distributed random numbers X~N(0,1) with randn. Then you can change the mean and standard deviation to be random. As an example:

N = 1000;
mu = rand*10 - 5;            %# mean in the range of [-5.0 5.0]
sigma = randi(5);            %# std in range of 1:5
X = randn(N, 1)*sigma + mu;  %# normally distributed with mean=mu and std=sigma

You must explain what distribution you want of the mean. Sure, you want it random, but there is order even in randomness. Please explain.

If you want a normally distributed mean, you can scale the variables [z = (x - mu) / sigma] and change mu randomly. If you need another distribution for the mean, similar formulas exist.

Again, please explain further.

Instead why don't you just generate random numbers, and change the generated range of numbers based on where the mean is at vs where you want the mean to be?

What other properties do you want? You can do something like this:

nums = (rand(1000,1)+rand())/2

That will shift your array a random number, also shifting the mean. This would keep the same standard deviation though. Something like this would change both:

nums = (rand(1000, 1)*rand()+rand())/2

if you want the sample to have exactly a given mean mu, then I would force the sample mean to be that value:

x = some_random_variable_generator(arguments);
x = x + (mu - mean(x));

then you're done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!