Random display of 'even' or 'odd'

13 visualizzazioni (ultimi 30 giorni)
Mahad Aftab
Mahad Aftab il 8 Nov 2019
Modificato: Adam Danz il 13 Gen 2020
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 Commenti
Steven Lord
Steven Lord il 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
Mahad Aftab il 9 Nov 2019
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.

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 9 Nov 2019
Modificato: Adam Danz il 14 Nov 2019
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 Commenti
Mahad Aftab
Mahad Aftab il 20 Nov 2019
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
odd
even
odd
odd
odd
head
head
odd
odd
i.e. according to my requirement
Adam Danz
Adam Danz il 20 Nov 2019
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.

Accedi per commentare.

Più risposte (1)

Praveen Iyyappan Valsala
Praveen Iyyappan Valsala il 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
EHB2A.png
source: https://math.stackexchange.com/questions/259359/how-random-is-the-digits-of-pi
  2 Commenti
Mahad Aftab
Mahad Aftab il 9 Nov 2019
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
Adam Danz il 9 Nov 2019
Modificato: Adam Danz il 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"

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by