Can someone help me understand this code?

3 views (last 30 days)
David Deman
David Deman on 7 Dec 2021
Edited: Voss on 18 Sep 2023
throwAgain=MCO_throwAgain()
function throwAgain=MCO_throwAgain()
die=1;
%Random number 1-6 of a 1x6 vector
RV=randi([1,6],1,6);
flag=0;
while(1)
for i=1:6
if(sum(RV==i)>=5)
flag=1;
break;
end
end
if(flag==1)
break;
end
die=die+1;
RV=randi([1,6],1,6);
end
throwAgain=die;
end
Thank you!
  2 Comments
khalaf Teleb
khalaf Teleb on 18 Sep 2023
Edited: Voss on 18 Sep 2023
Can someone help me understand this code?
function mls_output=mls_generator()
n= input('Enter the order of sequence, n=','s');
random_flag= input ('Enter the random_flag, random_flag= ', 's');
n=str2double(n);
random_flag=str2double(random_flag);
n_big=256; %any MLS larger than this value will be split into seperate files
if n<2
error('The input order must be 2 or greater')
end
if nargin==1
random_flag=0;
end
%generate register sequence
%--------------------------
if random_flag==0 %all ones
register_list=ones(1,n);
elseif random_flag==1 %random
register_list=round(rand(1,n));
while sum(register_list)==0 %make sure at least one element is non-zero
register_list=round(rand(1,n));
end
else %illegal input for random_flag
error('random_flag must be either set to zero or one')
end
prim_poly=gfprimdf(n); %obtain the primative polynomial
sum_list=find(prim_poly(1:n)==1); %determine elements from register sequence to be summed
if n<=n_big
mls_output=zeros(1,2^n-1); %generate empty sequence
for m=1:2^n-1 %fill the sequence
%the first element of the register sequence is stored
mls_output(m)=register_list(1);
%shift the register sequest and determine the new element from the
%primitate polynomial:
register_list=[register_list(2:n) mod(sum(register_list(sum_list)),2)];
end
else
disp('The specified order is large. Output will be saved into seperate files and run time will be long.')
split_number=(n+1)-n_big;
for p=1:split_number
if p==split_number
mls_output=zeros(1,2^n_big-1); %generate empty sequence
for m=1:2^n_big-1
%the first element of the register sequence is stored
mls_output(m)=register_list(1);
%shift the register sequest and determine the new element from the
%primitate polynomial:
register_list=[register_list(2:n) mod(sum(register_list(sum_list)),2)];
end
else
mls_output=zeros(1,2^n_big); %generate empty sequence
for m=1:2^n_big
%the first element of the register sequence is stored
mls_output(m)=register_list(1);
%shift the register sequest and determine the new element from the
%primitate polynomial:
register_list=[register_list(2:n) mod(sum(register_list(sum_list)),2)];
end
end
eval(['mls_output_',num2str(p),'of',num2str(split_number),'_order',num2str(n),'=mls_output;']);
disp(['Saving file ',num2str(p),' out of ',num2str(split_number),'.'])
save(['mls_output_',num2str(p),'of',num2str(split_number),'_order',num2str(n)],...
['mls_output_',num2str(p),'of',num2str(split_number),'_order',num2str(n)]);
end
mls_output='saved as files';
end
John D'Errico
John D'Errico on 18 Sep 2023
I'm sorry, but adding a question as a comment to another unrelated question is a bad idea. Worse, asking for help in understanding a lengthy code like that is asking too much, as we would need to write a compelte user's manual for MATLAb to do that. You just need to learn MATLAB.

Sign in to comment.

Answers (2)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh on 7 Dec 2021
function throwAgain=MCO_throwAgain()
define a function named MCO_thorAgain with no input and one output as throwAgain
die=1;
define and set the die vairable to 1 (as a scalar variable)
RV=randi([1,6],1,6);
create an 1x6 Array with integer random numbers between 1 and 6.
flag=0;
define and set the flag vairable to 0 (as a logical vairable)
while 1
will start a while loop and the condition is always true. but in code there is a flag which become 1 and the while loop breaks.
for i=1:6
if(sum(RV==i)>=5)
flag=1;
break;
end
end
in each loop the code check for every number between 1 to 6, if number of random numbers in RV which are equal to number i (each number) is greater or equal to 5. in other word the code check if 5 or 6 number of RV is same. so if it is the flag become 1 and the for-loop breaks.
if(flag==1)
break;
end
die=die+1;
RV=randi([1,6],1,6);
end % end of while
after for-loop ends, the code check if flag is 1. if it is the while loop breaks. if it isn't the scalar die increased by 1 value and RV become new random integer numbers. and while loop continue.
throwAgain=die;
end % end of function
when while loop end. the output of function become the die variable.
which means the output of function is number of times the random integer created but it does not have 5 or 6 same value in it. (first time in loop that RV has 5 or 6 same value the flag become 1 and loop breaks.
throwAgain=MCO_throwAgain()
this line just call the function to execute.

Chunru
Chunru on 7 Dec 2021
[throwAgain, RV]=MCO_throwAgain()
throwAgain = 35
RV = 1×6
4 4 4 4 4 1
function [throwAgain, RV]=MCO_throwAgain()
% MCO_throwAgain outputs the number of random throws taken to have at least 5 same
% die points out of 6 dies. Optionally it also outputs the final throw
% points.
die=1; % 1st throw
%Random number 1-6 of a 1x6 vector
RV=randi([1,6],1,6); % throw of 6 dies
flag=0; % at least 5 same points
while(1) % keep trying
% check if at least 5 dies have same point
for i=1:6
if(sum(RV==i)>=5)
flag=1;
break;
end
end
% if yes, stop
if(flag==1)
break;
end
% Otherwise, throw again
die=die+1;
RV=randi([1,6],1,6);
end
throwAgain=die; % retrurn the number of trials
end

Categories

Find more on Just for fun in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!