MATLAB generate random numbers

2019-01-15 22:34发布

问题:

Hi I am trying to generate random numbers in MATLAB with a random MEAN value.

For example, if I use

e = mean(rand(1000,1))

the answer for e will always be close to 0.5. What I want is for the value of e (mean) to be random, so that e can be 0.1, 0.2, 0.3, etc...

Is it correct for me to use e = mean( unifrnd(0,1,[1000,1]) ) ?

Thanks

回答1:

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


回答2:

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.



回答3:

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?



回答4:

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


回答5:

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.