Create a function randpi that will continuously generate random digits until pro
ID: 3838385 • Letter: C
Question
Create a function randpi that will continuously generate random digits until producing a sequence which reproduces up to n significant figures, where n is the input argument to the function. The function should print the total number of random digits that were generated in the process.
For example, if the call to the function is
>>randpi(3)
digits are to be randomly generated until the sequence 3 1 4 is produced, which corresponds to with 3 significant figures, i.e., 3.14.
For sake of illustration, shown is a stream of randomly generated digits terminating at 3 1 4:
62942241613494624558375764618056730468499330976020373824054965418834695314
In this particular case, 74 digits were generated prior to reaching the desired sequence. The number 74 would then be output on the terminal.
Explanation / Answer
I have changed the code in MATLAB, please check :
PI = 3.14159;
random = [];
n = input("Enter the value of n : ");
pi = PI * power(10, n-1);
integ = fix(pi);
finalNumber = 0;
fprintf("Random Number Generator termination at %d " , integ);
while finalNumber != integ
for i = 1:n
indx=randperm(9);
random(i) = indx(i);
fprintf("%d", random(i));
end
finalNumber = str2num(strrep(num2str(random), ' ', ''))
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.