MATLAB Write a script that will generate a random number n i

MATLAB- Write a script that will generate a random number \'n\' in the range -100 to 100 then attempt to \"guess\" what the value of n is by repeatedly generating a new random number in the same range until the two match. The script should then print how many tries it took to guess \"n\" correctly, as well as the value of n.

For example:

>> guess_n

It took 15 tries to guess n = 27

>> guess_n

It took 97 tries to guess n = -41

Solution

%matlab code

a = -100;
b = 100;

% generate first randon number
random1 = randi(b-a) + a;

countGuess = 0;

while true
% increment total guesses
countGuess = countGuess + 1;
% guess the number
random2 = randi(b-a) + a;
% check if both numbers are equal
if random1 == random2
break;
end
end

fprintf(\'It took %d tries to guess n = %d\ \', countGuess, random1);

%output: It took 13 tries to guess n = -72

MATLAB- Write a script that will generate a random number \'n\' in the range -100 to 100 then attempt to \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site