Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Generate a Random number between -1 and 1 with mean 0

Subject: Generate a Random number between -1 and 1 with mean 0

From: Natialol

Date: 18 Sep, 2011 20:10:28

Message: 1 of 5

Hi,

I need help generating a random number between -1 and 1 with a specified mean and variance.

I'm using c=a + b*randn() with 'a' as the mean and b as the standard deviation but the problem is that since the randn function only generates a mean zero standard deviation 1 random variable then I might end up having c greater or less than 1.

Thanks

Subject: Generate a Random number between -1 and 1 with mean 0

From: Wayne King

Date: 18 Sep, 2011 20:20:28

Message: 2 of 5

"Natialol" wrote in message <j55j7k$aj0$1@newscl01ah.mathworks.com>...
> Hi,
>
> I need help generating a random number between -1 and 1 with a specified mean and variance.
>
> I'm using c=a + b*randn() with 'a' as the mean and b as the standard deviation but the problem is that since the randn function only generates a mean zero standard deviation 1 random variable then I might end up having c greater or less than 1.
>
> Thanks

Hi, You did not state what distribution you want the random numbers to follow. You can use randn() to generate Gaussian random variables and scale it by the appropriate factor to reduce the variance sufficiently.

Alternatively, you can use rand() to generate uniform random variables.

Wayne

Subject: Generate a Random number between -1 and 1 with mean 0

From: Bruno Luong

Date: 18 Sep, 2011 21:48:13

Message: 3 of 5

"Natialol" wrote in message <j55j7k$aj0$1@newscl01ah.mathworks.com>...
> Hi,
>
> I need help generating a random number between -1 and 1 with a specified mean and variance.
>
> I'm using c=a + b*randn() with 'a' as the mean and b as the standard deviation but the problem is that since the randn function only generates a mean zero standard deviation 1 random variable then I might end up having c greater or less than 1.

I'm almost sure you will like this solution, despite the fact that it meets all the requirements:

% Specify standard deviation and mean
s = 0.5;
m = -0.3;

% Engine
p = 0.5*(1-1/sqrt(1+(s/m)^2));
if m<0
    p = 1-p;
end
q = s/(2*sqrt(p*(1-p)));
% generate 1e6 random numbers
x=q*(2*(rand(1,1e6)>=p)-1);

% Check
mean(x)
std(x)

% Bruno

Subject: Generate a Random number between -1 and 1 with mean 0

From: Roger Stafford

Date: 19 Sep, 2011 16:54:10

Message: 4 of 5

"Natialol" wrote in message <j55j7k$aj0$1@newscl01ah.mathworks.com>...
> I need help generating a random number between -1 and 1 with a specified mean and variance.
> ........
- - - - - - - - - -
  If you want a continuous distribution which lies strictly within [-1,1] with a specified mean and variance, you might consider the beta distribution. See the Wikipedia article at:

 http://en.wikipedia.org/wiki/Beta_distribution

  Using the formulas for mean and variance in that article to determine the corresponding alpha and beta parameters, this distribution can be generated in matlab with the use of the Statistics Toolbox function 'betainv' together with an appropriate use of the 'rand' function. You must of course adjust from your stated range [-1,1] over to the standard beta range [0,1].

Roger Stafford

Subject: Generate a Random number between -1 and 1 with mean 0

From: Bruno Luong

Date: 19 Sep, 2011 18:56:29

Message: 5 of 5

Beside the beta indicated by Roger, you might be interested in another continuous distribution, as indicated in my comment from Aug 26th of this FEX
http://www.mathworks.com/matlabcentral/fileexchange/23832-truncated-gaussian

% Save this function in a file:
function deltamean = meanTG(xbar, meantarget, stdtarget, range)

[~, meaneffective] = TruncatedGaussian(stdtarget, range-xbar, 0);
deltamean = meantarget - (meaneffective + xbar);

end % meanTG

%%%%%%%%%%%%%%%
% Call fzero
meantarget = -0.2;
stdtarget = 0.5;
range = [-1 1];

xbarstart = 0; % might require to be adjusted depending on mean and std
xbar = fzero(@(xbar) meanTG(xbar, meantarget, stdtarget, range), xbarstart)

% Create a shift TrunccatedGaussian
X = xbar + TruncatedGaussian(stdtarget, range-xbar, [1 1e6]);

% Check
fprintf('mean(X) = %f\n', mean(X));
fprintf('std(X) = %f\n', std(X));
fprintf('min(X) = %f\n', min(X));
fprintf('max(X) = %f\n', max(X));
hist(X)

% Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us