Random display of 'even' or 'odd'
Show older comments
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 Comments
What do you mean "writing toss" and "even/odd should be displayed"?
Please provide clear examples of inputs and expected outputs - neither are clear right now.
- 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?
Steven Lord
on 8 Nov 2019
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?
Mahad Aftab
on 9 Nov 2019
Accepted Answer
More Answers (1)
Praveen Iyyappan Valsala
on 8 Nov 2019
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 Comments
Mahad Aftab
on 9 Nov 2019
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"
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!