MATLAB: Random display of ‘even’ or ‘odd’

MATLABmatlab functionrandomstatement

Hey Everyone I will be highly grateful if you help me out. So I have been assigned to design a function which will generate 'even' or 'odd' in output once toss is written in command window. And this generation should be based on the number is pi.
For Example, if 3.14159265359 are the numbers in pi. then by writing 'toss' one time odd should be displayed. After writing 'toss' the second the 'even' should be displayed and so on.

Best Answer

Here are the 5 steps you need to take. Since this is your assignment, this answer isn't complete. Each step shows what you need to do in that step but you need to adjust it to your needs. Please feel free to leave a comment if you get stuck.
% Step 1: use rem() to isolate the decimals of a number.
% In this example, I isolate the decimals of sqrt(2)
d = rem(sqrt(2),1);
% Step 2: extract each number 1-to-n following the decimal point.
% In this example we extract the decimals of 1/12.
% The result is a char array
n = 8;
dvec = regexprep(num2str(1/12, sprintf('%%.%df', n)),'^\d+.', '');
% Step 3: isolate the characters of a char array and convert to numeric vector
nv = str2double(num2cell('12345'));
% Step 4: determine of each element of a numeric vector is even/odd
% 'isOdd' will the true for each element that is odd
isOdd = mod([9 8 7 6 5], 2);
% Step 5: convert vector of True/False to 'Odd'/'Even' (where true=Odd)
oddEven = {'Odd','Even'};
logicalVector = [true true false true false false false];
out = oddEven(~logicalVector+1)
Update: Return a random char array: Even or Odd
after further clarification in the comments below, this is the function you're looking for. The function does not have any inputs. Just call toss() and it will return the character array "EVEN" or "ODD"
function y = toss()
options = {'EVEN','ODD'};
y = options{randi([1,2],1)};
% If you want to print the output to the command line

fprintf('%s\n',y)
Example
toss()
Alternatively, the first input could be a positive integer that determines the number of random "even" "odd" outputs.
function y = toss(x)
options = {'EVEN','ODD'};
y = options(randi([1,2],1,x));
% If you want to print the output to the command line
disp(y)