1

First question: I am trying to generate random integers with random digits into an array. However, since boundary value of integer varies as 'int8', 'int16','int32', 'int64' and unsigned versions of them, I cannot decide which type I should use to generate my simple array as below. See: https://www.mathworks.com/help/matlab/ref/intmax.html

When I use randi(n, row, column) where n is the upper bound integer value, row is row number and column is column number. However, this does not satisfy my purpose of solving my problem. So, which form of "rand" function should I use?

A= [2 7 43 556 32 35 3 4566]

Second question: If I can achieve my purpose in the first question, how do I iterate over each number in the array? I want to check each number digit by digit and respectively. Like in the example array, it should first check 2, and 7, and 4 and 3, and 5, and so on.

Any help would be appreciated.

Özge
  • 59
  • 7
  • "Random integers" and "with random digits" are synonymous to me, but perhaps they mean something different in your application. Could you explain exactly what you're trying to do? Are you having difficulty in deciding on an integer type because of the limits of the integer values? What are the limits in your case? Why does specifying an upper bound, row and column not satisfy your purpose? What exactly is your purpose and your desired output? – beaker Nov 03 '17 at 14:23
  • @beaker I would like to generate an array as I showed in the example actually. The number of digits can be 2,3,4 or more. So, I will take a user input which will decide the length of the array. By using this input I will determine the column number of the array and row number always going to be 1. – Özge Nov 03 '17 at 14:35
  • Okay, so you want random integers, each with a random *number* of digits? What is the range of the number of digits you want? Can the integers be negative? – beaker Nov 03 '17 at 14:39
  • Numbers can be 0 or positive integers. – Özge Nov 03 '17 at 14:43
  • It actually works but since I could not decide whether I should use uint8 or int32, I was confused. I fixed it right now. Thank you! Also I wonder why this question got -1? – Özge Nov 03 '17 at 18:48

1 Answers1

0

To generate random numbers in the full range of a given data type, you can use the following:

n = int32(0);
type = class(n); % or you can specify it explicitly e.g. 'uint8' directly
randSize = [10 1];
randomArray = randi([intmin(type) intmax(type)],randSize,type);

To iterate through the digits, you can isolate each digit with a combination of division and modulo. Probably not the most elegant solution, but it does the job. I'm not sure how you would like to handle the sign for your particular application, so I simply ignored it using absolute value:

for i = abs(randomArray(:))'
    numberOfDigits = ceil(log(double(abs(i)+1))/log(10)); % use log 10 to count digits
    fprintf('\nisolating every digit for number %i: ',i);
    for digitId = numberOfDigits:-1:1
        digitFactor = 10^(digitId-1);
        digitBeforeDivision = (i - mod(i,digitFactor));
        digit = digitBeforeDivision / digitFactor;
        fprintf('%d ',digit);
        i = i - digitBeforeDivision;
    end
end
Jean B.
  • 46
  • 3
  • This was a very advanced approach for me but it did work. How did you come up with this solution? Could you explain your idea by implementing this code? – Özge Nov 03 '17 at 18:51
  • Let's say the number is 225. This number has 3 digits because `log_10(225) = 2.35` (rounded up = 3). The first digit is obtained by doing `(225 - 25) / 100 = 2`. You get 25 with `225 modulo 100 = 25`, and 100 is 10 to the power of the position `10^2 = 100`. Also, I fixed a small bug that would cause numbers like 10000 to fail. – Jean B. Nov 03 '17 at 19:13