Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here\'s a question with regard to While Loop in Matlab: Write a function to comp

ID: 3638078 • Letter: H

Question

Here's a question with regard to While Loop in Matlab:

Write a function to compute the average of random numbers as follows. The function receives the quantity of numbers to be averaged. Use the statement rand(size, 1) to create an array of random numbers where the value of size determines how many numbers will be in the array. For example,
num = rand(5, 1)
would create an array with five random numbers. Display the array values by not including a semicolon. Using a “for” loop, compute the average of all the numbers. Do not use a built-in function for this. Remember that the average is the sum divided by the quantity of values

Explanation / Answer

Random number stream To create a single random number stream, use the RandStream constructor. To create multiple independent random number streams, use RandStream.create. The rng function provides a simple interface to create a new global stream. stream = RandStream.getGlobalStream returns the global random number stream, that is, the one currently used by the rand, randi, and randn functions. prevstream = RandStream.setGlobalStream(stream) designates the random number stream stream as the new global stream to be used by the rand, randi, and randn functions, and returns the previous global stream. Save and restore the current global stream's state to reproduce the output of rand: stream = RandStream.getGlobalStream; savedState = stream.State; u1 = rand(1,5) u1 = 0.8147 0.9058 0.1270 0.9134 0.6324 stream.State = savedState; u2 = rand(1,5) u2 = 0.8147 0.9058 0.1270 0.9134 0.6324 u2 contains exactly the same values as u1.