Tan Wei Jin
Tan Wei Jin

Reputation: 21

MATLAB generate random numbers

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

Upvotes: 0

Views: 9776

Answers (5)

shabbychef
shabbychef

Reputation: 1999

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.

Upvotes: 0

Dan Lorenc
Dan Lorenc

Reputation: 5394

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

Upvotes: 0

Escualo
Escualo

Reputation: 42152

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.

Upvotes: 4

Amro
Amro

Reputation: 124563

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

Upvotes: 3

Sneakyness
Sneakyness

Reputation: 5403

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?

Upvotes: 0

Related Questions