0

I use the code as follow to generate a random number between 10000 and 60000.

(int)round(1.0 * rand() / RAND_MAX * 50000 + 10000)

My Question is why 1.0 * rand(), and what is the difference with only rand(), not multiply 0.1?

Your answers are highly appreciated!

Gino
  • 1
  • 1
  • 3
    It's just a quick way of converting the integer returned by `rand` to a double for the division to work, but you could just as well convert it to a double explicitly. – Ted Klein Bergman Jan 06 '22 at 11:43
  • 6
    you'd be better off using c++11's [random](https://en.cppreference.com/w/cpp/numeric/random), see https://stackoverflow.com/questions/52869166/why-is-the-use-of-rand-considered-bad. e.g. in visual studio `RAND_MAX` is only 32,767 meaning your numbers will be really poorly distributed – Alan Birtles Jan 06 '22 at 11:45
  • Just a bad way to force a cast to double to avoid integer division. –  Jan 06 '22 at 11:48
  • 1
    Try doing it without the `1.0 *` and see if you actually get random numbers. – cigien Jan 06 '22 at 11:50
  • @YvesDaoust What's your objection to it? – Bathsheba Jan 06 '22 at 11:52
  • @Bathsheba: 1) readability: a cast would tell the truth and not puzzle the OP, 2) efficiency: unless the compiler notices, the implementation involves a cast followed by a useless multiply. –  Jan 06 '22 at 11:56
  • 1
    @YvesDaoust: Clearly we'll have to agree to disagree but in intense mathematical code, `1.0 * ` adequately explains what you're doing, and `static_cast<>` can be a pain in the neck in complicated mathematical expressions and is occasionally harmful as a hardcoded type in `<>` can be vulnerable to errant refactoring. (2) check your compiler is not doing something silly. gcc and msvc with optimisations set to maximum don't. – Bathsheba Jan 06 '22 at 11:58
  • 1
    @YvesDaoust -- concerning terminology, there are no casts in the code in the question; there is an implicit **conversion**. A cast is something you write in your source code to tell the compiler to do a conversion. That `1.0 * rand()` doesn't "force a cast"; it forces a **conversion**. – Pete Becker Jan 06 '22 at 14:04
  • @PeteBecker: That silly multiply by 1.0 is used *instead of a cast*, this is why I said that it *forces* a cast. And concretely, there is little difference between a cast from int to double and a conversion from int to double. –  Jan 06 '22 at 14:26
  • @YvesDaoust -- again -- it does not force a **cast**, it forces a **conversion**. They are two completely different things. A cast is something you write in your source code to tell the compiler to do a conversion. Concretely there is a **vast** difference. `int f = (int)1.0;` does a conversion and uses a cast. `int f = 1.0;` does a conversion and does not use a cast. – Pete Becker Jan 06 '22 at 14:42
  • @YvesDaoust: What an odd thing to say! Out of interest what do you think of the `+ 0.0 ` idiom to remove a signed negative zero? Something that comes up time and time again in mathematical code are things like `e[i][j][k] = 1.0 * (i + j + k) / N * phi;`. Of course and lamentably `std::vector` permits floating point values for vector indices (truly ghastly), whereas pure arrays don't. What do you suggest for that sort of expression? – Bathsheba Jan 06 '22 at 14:44
  • 1
    @Bathsheba: `double(i + j + k) / double(N) * phi` would be fine. –  Jan 06 '22 at 14:55
  • @YvesDaoust: I don't like it. Two occurrences of `double` - that's a refactoring vulnerability. And it looks less like the formula in the supporting mathematics paper. I've been there when looking at converting `double` to `decimal`. Let's agree to disagree. – Bathsheba Jan 06 '22 at 15:09

0 Answers0