views:

770

answers:

5

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

A: 

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?

Sneakyness
hi, thanks for the quick response. how to i determine where the mean is at? i want the mean to be random can be 0.9 or can be 0.1 not centred around 0.5...i think it has something to do with distribution but im not sure
Tan Wei Jin
Oh I thought you were trying to generate random numbers that made a specific mean.
Sneakyness
A: 

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
Dan Lorenc
hi,i tried nums =mean( rand(1000,1)+rand()).i performed the calculation many times and sometimes i get a value of mean of >1. shouldn't the value of mean be between 0 and 1 if im generating random numbers between 0 and 1
Tan Wei Jin
Yes, but you're adding two random numbers together, so you can get some higher than 1. If you want to constrain your results to be between 0 and 1, try the new edit
Dan Lorenc
+3  A: 

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
Amro
+3  A: 

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.

Arrieta
A: 

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.

shabbychef