I mostly agree with lastchance, but I would personally just do
round(255.0 * (1.0 + sin(x)) / 2.0); This also would avoid the problem when sin(x) == 1.0.
also I see a lot of people putting 1 + before Rand() why is this done |
For things like dice rolling, because rand() delivers a range of [0, RAND_MAX], doing rand() % 6 then delivers a range [0, 5], and +1 is added to make it the actual dice numbers (range 1 to 6, inclusive).
Obligatory note that it's possible for rand() to not be very uniform, and if the % operator is used in combination with rand, that can skew the distribution as well so that it's less uniform (doesn't matter for most simple games, though can matter for scientific simulations).
<random> contains improved C++11 random utilities
http://www.cplusplus.com/reference/random/
but why the need to multiply it by 0.00002 I'm guessing this slows it down exponentially? |
Well, it would slow it down
linearly by 0.00002 :)
That's just a magic number that the author decided to make the frequency feel right. It depends on how often that loop is being processed (60 fps? not capped? etc.).
When it comes to games, something called a
timestep can be very important, to make sure the game's logic doesn't speed up or slow down depending on your framerate -- with many older games (like '90s N64), the game itself would slow down when rendering a large number of items. This doesn't usually happen in modern games, though.
https://gafferongames.com/post/fix_your_timestep/