1

I understand that random number generators use the modulo operator to generate a random number within a range. What I am curious is why is it better to use that than division. For example, I could generate a random number in the range of min to max by using the equation:

(max-min) * random_number/maximum_possible_number  + min  

where maximum_possible_number is the largest possible number that can be represented.
This works because random_number/maximum_possible_number generates a number between 0 and 1. When that's multiplied by max-min it is a number between min and max.

Why is using a modulo algorithm better than this algorithm?

Edit:
To test this algorithm I wrote the following Matlab code to randomly generate 10000 numbers between 0 and 1 bit by bit and plot them:

clear all;

numBits = 32;
numbersToGenerate = 10000;

% Generate 10000 random numbers between 0 and 1
for i = 1:numbersToGenerate
    bits = randi([0 1], numBits, 1);
    s = 0;
    maxNumber = 0;
    for bit = 1:numBits
        s = s + bits(bit)*2^bit;
        maxNumber = maxNumber + 2^bit;
    end

    number(i) = s/maxNumber;
end


% Break into sections and count numbers within each section
size = .01;
for s = 0:size:1-size
    sections(int8(s/size)+1) = sum(number>s & number<s+size);
end

plot (0:size:1-size, sections);
xlabel('Number');
ylabel('Count');

The output looks like this: enter image description here

Edit2: (To give a more detailed explanation about what is happening in my code). I generate 10000 random numbers. This is done by generating 32 bits using the randi() function (for each number). While this is being done the largest possible number is also generating (32 1's in a row). Then the random number is calculated by dividing the random 32 bits by the largest possible number (32 bits of 1).

Tyler Hilbert
  • 2,107
  • 7
  • 35
  • 55
  • I misread your question at first -- I though that you were talking about using division instead of modulo in the *definition* of a PRNG (which would be a catastrophically bad idea). If you have random integers and you want to get random floats (or random integers in another range) then *neither* modulo nor division are good ideas. The former suffers from *modulo bias* and the latter suffers from round-off error. Both can cause a noticeable departure from uniformity. See [this answer](https://stackoverflow.com/a/6852396/4996248) for a nice discussion. – John Coleman Apr 02 '18 at 19:26
  • That was an interesting read. Correct me if I'm wrong, but don't most algorithms that generate a random number within a range use the modulo operator? That's how we were taught in class – Tyler Hilbert Apr 02 '18 at 19:38
  • 1
    There is a difference of perspective between someone who needs to write a library (hence needs to worry about minor bias) and someone who just wants to do write some quick code. For 32-bit random numbers, the biases that you might get with using either modulus or division are minor. I have seen both approaches used in code (e.g. dividing by `RAND_MAX` or `RAND_MAX+1` is fairly common in older C code). [Here](https://stackoverflow.com/q/10219355/4996248) is another interesting discussion. – John Coleman Apr 02 '18 at 20:11

0 Answers0