Quote:Original post by DarkHorizon
Using rand()%1000 however, would return a random number between 1 and 1000.
Keep in mind, though, that can hurt the distribution and random-ness. You'd be best off scaling the range [0, RAND_MAX] to [0, N], if N happens to be small then the modulo operation can be have satisfactory results however.
Quote:Original post by freeworld
curious off the tops of your heads, when else could the '%' operator come in handy?
If you wanted to simulate continuous space that wraps around the screen - like in Asteriods - then you could use the modulo operator to make entities appear on the opposite side of the screen whenever they fly off the edge.
Ex:
ship_x = (ship_x + velocity_x * time) % screen_width;
ship_y = (ship_y + velocity_y * time) % screen_height;
// N.B doesn't handle what happens if ship_x or ship_y are less than zero.