MATLAB Answers


Random display of 'even' or 'odd'

Mahad Aftab さんによって質問されました 2019 年 11 月 8 日
最新アクティビティ Adam Danz
さんによって コメントされました 2019 年 11 月 20 日 17:43
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 件のコメント

Adam Danz
2019 年 11 月 8 日
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
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?
Mahad Aftab 2019 年 11 月 9 日
Yes it is a kind of assignemnt. The input at user end is "toss" and the expected output is not a number but the word "even" or "odd" should be displayed at output.
The example of rand() if we write rand() it gives a random number. If we write rand() again it gives an other random number. Similarly I want to generate the string "odd" or "even" as output. And the sequence of this generation should be according to the first 20 decimals of pi. I hope I am clear this time. Since I am new to MATLAB your help would really guide me around.

サインイン to comment.

2 件の回答

Adam Danz
回答者: 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
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

  6 件のコメント

Adam Danz
2019 年 11 月 14 日
See updated answer.
If this has been helpful, please consider accepting the answer (blue button).
Mahad Aftab 2019 年 11 月 20 日 17:34
Ok Thanks alot for your answer I am a step closer to my assigment. Can you please guide me how I can control this random generation? Like if I want
i.e. according to my requirement
Adam Danz
2019 年 11 月 20 日 17:43
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.

サインイン to comment.

回答者: 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
If you want trouble:
pi_st=(num2str(sprintf('%1.32f',pi))); % pi value string
pi_st(2)=[]; %remove .
if(mod(random_pidigit,2)==0); fprintf('Even\n'); else fprintf('Odd\n'); end
Histogram of the first 10000 digits

  2 件のコメント

Mahad Aftab 2019 年 11 月 9 日
I really appreciate your assiatance but what I am actually trying to design is that:
If i give input, for the first time "toss", the output should display the word "even"
If i give input "toss", the second time, then output should display word "odd".
and so on...
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"

サインイン to comment.

Translated by