-2

Below I am going to show two different versions of rand() implementations.

First, the modulo operator (%):

int r = rand() % 10;

Now, I know that this statement produces a random integer between 0-9.

Second, the multiplication operator(*):

double r = rand() * 101.0 / 100.0;

This is the one I am confused about. I have seen this implementation once before but I cannot find a good explanation of what the 'rules' are here. What are the bounds/restrictions? What is the purpose of the division instead of just typing:

double r = rand() * 1.0;
  • Does this answer your question: https://stackoverflow.com/q/52869166/430766 It is highly related but I don't think a dupe. – bitmask Oct 26 '22 at 13:31
  • 1
    There is nothing special about using `*` and `%` "when using the rand() function". So I'm not sure if your question is "what does `rand()` actually return" or "what do the modulo and multiplication operators actually do" – Nathan Pierson Oct 26 '22 at 13:32
  • 3
    To state the obvious: One difference between `rand() * 101.0 / 100.0` and `rand() * 1.0` is that `101.0 / 100.0` is `1.01`, not `1.0`. – Nathan Pierson Oct 26 '22 at 13:34
  • 3
    This might be even more related: https://stackoverflow.com/q/10984974/430766 It explains in detail why the `rand() %` idiom is really bad. – bitmask Oct 26 '22 at 13:34
  • 1
    The second code line doesn't make any sense whatsoever. Are you sure that uses the ```rand()``` from the standard C library, not some custom ```rand()```? The division that you normally see is something like ```(double) rand() / RAND_MAX``` to get a floating point number between 0 and 1. – Homer512 Oct 26 '22 at 13:35
  • @bitmask -- `rand() % 10` is pretty good; slightly biased, but not enough to matter in beginner programs. For beginners struggling to get code to work, fixing the bias is a major distraction. – Pete Becker Oct 26 '22 at 14:26
  • @PeteBecker This might be a philosophical dispute, but why learn a technique that is broken, when a stable alternative is available? Especially since understanding why the modulo is bad teaches you a number of interesting things. Furthermore `rand` itself is bad. No useful guarantees whatsoever. – bitmask Oct 26 '22 at 14:29
  • @bitmask -- it's not broken. For many things it works just fine. Unfortunately, programmers often fall into the "if it's not perfect it's useless" fallacy. – Pete Becker Oct 26 '22 at 14:44
  • 1
    In C++, [rand() is considered harmful](https://www.youtube.com/watch?v=LDPMpc-ENqY) – prapin Oct 26 '22 at 18:25

1 Answers1

0

There is no different implementations for rand() function. The difference in these two cases is the mathematical operation you do with the value returned by rand() function.

In the first case you just take the number returned by rand() and divide it to 10 using modulo operator (so getting a number between 0 to 9). And in the second case you just multiplying it by 101 (or 1).

For example rand() has returned number 123. case first: 123 % 10 gives you 3 (the reminder of the normal division). case second: you just multiply 123 by 101 which is 12423.

Checkout this.