- Does this only operate on pi?
- how many dp of pi?
- is the leading 3 included?
- what do you mean by "writing toss"?
- How are the even and odd numbers displayed? As a vector? characters?
- Where are the outputs displayed, in the command window?
- What have your tried already and where are you in this process?
Random display of 'even' or 'odd'
14 次查看(过去 30 天)
显示 更早的评论
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.
3 个评论
Steven Lord
2019-11-8
If you're generating these numbers "randomly" based on the digits in pi (or π, to address Adam Danz's second question) they're not really random, are they?
Since I'm guessing this is a homework assignment, can you show us exactly what you're being asked to do?
采纳的回答
Adam Danz
2019-11-9
编辑:Adam Danz
2019-11-14
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)
6 个评论
Adam Danz
2019-11-20
Do you mean you don't want it to be random but you want to control whether even/odd or heads/tails appears? You could create an additional input to determine which value is provided in the output.
更多回答(1 个)
Praveen Iyyappan Valsala
2019-11-8
Pi has equal distribution of all digits i.e odd and even digits are uniformly distributed. so you can simply use rand function which generates uniformly distributed random number.
function toss
if(rand(1)<0.5); fprintf('Even\n'); else fprintf('Odd\n'); end
end
If you want trouble:
pi_st=(num2str(sprintf('%1.32f',pi))); % pi value string
pi_st(2)=[]; %remove .
random_pidigit=pi_st(randi(length(pi_st)));
if(mod(random_pidigit,2)==0); fprintf('Even\n'); else fprintf('Odd\n'); end
Histogram of the first 10000 digits
source: https://math.stackexchange.com/questions/259359/how-random-is-the-digits-of-pi
2 个评论
Adam Danz
2019-11-9
编辑:Adam Danz
2019-11-9
This doesn't match the description in your comment above (under the question).
Please provide examples of inputs and outputs instead of describing the goal.
For example.
Input: 5
output: "Odd"
Input: [1 2 3];
output: "Odd" "Even" "Odd"
Input: 3.141
output (ignoring numbers to the left of the decimal): "Odd" "Even" "Odd"
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!