I'm trying to generate random variables with command rndu, but I want to specify range and size.
I want x~uniform[-2,2], so I tried
state=-2; size=4;
{x, newstate}=rndu(100, 1, state};
But I only got nonnegative numbers for x.
Furthermore, I don't know how to combine size option with this command.
1 Answer
0
rndu
returns random numbers between 0 and 1.
The optional input, state
, allows you to pass in a state vector that controls the random number stream. It is like a bookmark for the stream of random numbers that rndu
will create.
If you want to change the range of the numbers, you need to multiply or add the results.
// 1. Draw 100 numbers between 0 and 1.
// 2. Move the mean to zero by subtracting 0.5
// 3. Move the range from -0.5, 0.5 to -2, +2 by
// multiplying by 4
x = (rndu(100,1) - 0.5) .* 4;
Your Answer
1 Answer
rndu
returns random numbers between 0 and 1.
The optional input, state
, allows you to pass in a state vector that controls the random number stream. It is like a bookmark for the stream of random numbers that rndu
will create.
If you want to change the range of the numbers, you need to multiply or add the results.
// 1. Draw 100 numbers between 0 and 1.
// 2. Move the mean to zero by subtracting 0.5
// 3. Move the range from -0.5, 0.5 to -2, +2 by
// multiplying by 4
x = (rndu(100,1) - 0.5) .* 4;