Got Questions? Get Answers.
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 random numbers over interval [0,pi]

Subject: generate random numbers over interval [0,pi]

From: John Wong

Date: 24 Feb, 2011 07:30:33

Message: 1 of 6

The assignment is to " Generate a string of 1000 random numbers delta(n) uniformly distributed over the interval [0, pi]. Build the strings x(n) = cos(delta(n)) and y(n) = sin(delta(n)).

Summation of x(n) with index n ---> X
Summation of y(n) with index n ---> Y

Find r = sqrt( X^2 + Y^2)

Compute the value of r if the string had 10,000 elements?

----
I am not sure if I should really ask for help. But anyway, the first problem I encounter is to generate the random numbers. I read the help and some other online sources, the rand() will generate rand(1000,1) on the interval [0,1]. If I want [0,10], I have to multiple the rand by 10.

10* rand(1000,1)
Q1: My question is, is it okay to do pi * rand(1000,1) because in some tasks we might have to convert the pi into something else.

Q2 is more like how to solve this assignment. It says "a string"? Should I read that as "vector", or "array" of 1000 random numbers????

My game plan is to have a simple iteration reading each n in the array, and then compute the summation of x(n), which is given as cos(delta(n)).....

Am I correct?????

I know I shouldn't be asking help with homework, but I guess some of the bright minds here can bolster my hope with this problem :]

Thanks.

Subject: generate random numbers over interval [0,pi]

From: John Wong

Date: 24 Feb, 2011 07:37:04

Message: 2 of 6

forgot to add my attempt

n = pi*rand(1000,1);

x = sum(cos(n))
y = sum(sin(n))

r = sqrt(x^2 + y^2)
  

Subject: generate random numbers over interval [0,pi]

From: John D'Errico

Date: 24 Feb, 2011 10:46:19

Message: 3 of 6

"John Wong" wrote in message <ik51ep$c3e$1@fred.mathworks.com>...
> The assignment is to " Generate a string of 1000 random numbers delta(n) uniformly distributed over the interval [0, pi]. Build the strings x(n) = cos(delta(n)) and y(n) = sin(delta(n)).
>
> Summation of x(n) with index n ---> X
> Summation of y(n) with index n ---> Y
>
> Find r = sqrt( X^2 + Y^2)
>
> Compute the value of r if the string had 10,000 elements?
>
> ----
> I am not sure if I should really ask for help. But anyway, the first problem I encounter is to generate the random numbers. I read the help and some other online sources, the rand() will generate rand(1000,1) on the interval [0,1]. If I want [0,10], I have to multiple the rand by 10.
>
> 10* rand(1000,1)
> Q1: My question is, is it okay to do pi * rand(1000,1) because in some tasks we might have to convert the pi into something else.
>

Yes, pi*rand(1000,1) will yield random numbers in the
desired interval. I'm not sure why you think this stops
you from choosing a different interval in the future on
a different problem. The next time you use rand, it will
again generate random numbers that are uniform in the
unit interval.

    pi*rand(stuff)

simply multiplies the output of the function rand by the
chosen constant.


> Q2 is more like how to solve this assignment. It says "a string"? Should I read that as "vector", or "array" of 1000 random numbers????
>

EXACTLY. The word "string" here is used as a synonym
for a sequence of numbers, in the form of a vector of
them in MATLAB.


> My game plan is to have a simple iteration reading each n in the array, and then compute the summation of x(n), which is given as cos(delta(n)).....
>
> Am I correct?????

No. I think you are mistaken here. n is used as simply an
index here.

    x(n) = cos(delta(n))

For a vector (named delta) of random numbers, uniformly
distributed over the desired interval [0, pi], compute the
cosine of each value, and stuff it into the vector x. So the
above statement simply is intended to mean that the n'th
element of x is the cosine of the n'th element of delta.

You could do the assignment as an explicit loop using for,
but in MATLAB, you would be better served to do this entire
operation as a simple call to cos(delta). This is called
vectorization. Operators like cos and sin are all vectorized,
so they can take entire vectors and arrays as inputs.


> I know I shouldn't be asking help with homework, but I guess some of the bright minds here can bolster my hope with this problem :]
>

I almost never respond to homework, because it is your
job to do, not mine. However, I will make an exception
for an individual who has done some thinking on the
subject.

HTH,
John

Subject: generate random numbers over interval [0,pi]

From: Steven_Lord

Date: 24 Feb, 2011 14:49:17

Message: 4 of 6



"John Wong" <gokoproject@gmail.com> wrote in message
news:ik51ep$c3e$1@fred.mathworks.com...
> The assignment is to " Generate a string of 1000 random numbers delta(n)
> uniformly distributed over the interval [0, pi]. Build the strings x(n) =
> cos(delta(n)) and y(n) = sin(delta(n)).
>
> Summation of x(n) with index n ---> X
> Summation of y(n) with index n ---> Y
>
> Find r = sqrt( X^2 + Y^2)
>
> Compute the value of r if the string had 10,000 elements?
>
> ----
> I am not sure if I should really ask for help. But anyway, the first
> problem I encounter is to generate the random numbers. I read the help and
> some other online sources, the rand() will generate rand(1000,1) on the
> interval [0,1]. If I want [0,10], I have to multiple the rand by 10.
> 10* rand(1000,1)
> Q1: My question is, is it okay to do pi * rand(1000,1) because in some
> tasks we might have to convert the pi into something else.

Hint: if you have a variable named upperBound in your workspace, this is a
perfectly valid MATLAB statement:

q = upperBound*rand(1000, 1);

> Q2 is more like how to solve this assignment. It says "a string"? Should I
> read that as "vector", or "array" of 1000 random numbers????

My interpretation of the assignment agrees with yours. I don't think the
author meant the result should be a char array but it should be a numeric
vector.

> My game plan is to have a simple iteration reading each n in the array,
> and then compute the summation of x(n), which is given as
> cos(delta(n)).....
>
> Am I correct?????

That's one way to do it, but remember that a lot of functions in MATLAB are
vectorized; that is they can operate on each element of a vector in one
call. This technique is used in the Example on the reference page for SIN:

http://www.mathworks.com/help/techdoc/ref/sin.html

> I know I shouldn't be asking help with homework, but I guess some of the
> bright minds here can bolster my hope with this problem :]
>
> Thanks.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: generate random numbers over interval [0,pi]

From: John Wong

Date: 24 Feb, 2011 15:36:19

Message: 5 of 6

>
> No. I think you are mistaken here. n is used as simply an
> index here.
>
> x(n) = cos(delta(n))
>
> For a vector (named delta) of random numbers, uniformly
> distributed over the desired interval [0, pi], compute the
> cosine of each value, and stuff it into the vector x. So the
> above statement simply is intended to mean that the n'th
> element of x is the cosine of the n'th element of delta.
>
> You could do the assignment as an explicit loop using for,
> but in MATLAB, you would be better served to do this entire
> operation as a simple call to cos(delta). This is called
> vectorization. Operators like cos and sin are all vectorized,
> so they can take entire vectors and arrays as inputs.


Hi John and Steven, thanks.
Since the random generator will create an array (I am not sure the difference between an array and a vector in MATLAB language... so I'd use array for now). This means when I compute the following I would get the desired result I think?

n = pi*rand(1000,1)


% More explicitly, like John you said

delta = pi*rand(1000,1)

cos(delta);
for n = 1: 1000;
    X(n) = cos(delta(n));
end

X(1000)
sum(X) % I just checked with Matlab this will give the same thing as the one below

x = sum(cos(delta))


So I guess I am on the right track? now I just need to perform the square root operation like I did previously?
:]
Thank you guys.

Subject: generate random numbers over interval [0,pi]

From: Think blue, count two.

Date: 24 Feb, 2011 18:42:33

Message: 6 of 6

On 24/02/11 4:46 AM, John D'Errico wrote:
> "John Wong" wrote in message <ik51ep$c3e$1@fred.mathworks.com>...
>> The assignment is to " Generate a string of 1000 random numbers
>> delta(n) uniformly distributed over the interval [0, pi].

> Yes, pi*rand(1000,1) will yield random numbers in the
> desired interval.

Caution: pi * rand(1000,1) will yield random numbers in the interval
(0,pi) rather than the desired [0,pi] .

The exact bounds of the random generators vary, but the default
generator (twister) generates a positive integer in the range 1 to
2^53-1 and divides the result by 2^53, giving an interval
[2^(-53),1-2^(-53)]

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