-6

I need to compute a randomly selected digit from a given four digit number. In this question, I use the terminology similar to “digit 3 of the number 7246 is 2” and “digit 1 of the number 7246 is 6”. That is, digit numbers count from the right, starting at 1.

Input variables:

  • number is a single four digit number.

Output Variables:

  • which_digit holds a random integer between 1 and 4 using randi
  • digit holds the which_digit digit of number.

A sample case is:

>> number = 7246;
>> rand_digit
which_digit = 3
digit = 2
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    What is your specific question/detail you need help implementing? – MichaelTr7 Feb 06 '21 at 07:00
  • 2
    Hi user15156627, welcome to StackOverflow. Please read [ask] and [How to Ask Homework Questions?](https://meta.stackoverflow.com/q/334822)). We are not here to do your homework but we can help you to do it – Sardar Usama Feb 06 '21 at 10:25
  • 1
    This question appears to be narrowly scoped, and focused. I'm voting to reopen. – cigien Feb 09 '21 at 10:47
  • 2
    Does this answer your question? [Generate a random number in a certain range in MATLAB](https://stackoverflow.com/questions/5077800/generate-a-random-number-in-a-certain-range-in-matlab) followed by [Matlab: Is there a function to locate an 'n' digit in a number (i.e 2nd digit in '75673', so 5)](https://stackoverflow.com/questions/26960213/matlab-is-there-a-function-to-locate-an-n-digit-in-a-number-i-e-2nd-digit-in) – Tomerikoo Feb 09 '21 at 10:53

1 Answers1

0

Combining the answers from:

You could do something like:

function [which_digit, digit] = randDigit(number)
which_digit = randi([1, 4]);
digit = floor(mod(number, 10^which_digit) / 10^(which_digit-1));
end
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61