Velocities mainly control the angel of the ball in our case.
For example, if the velocity on the x-axis is 0 and on the y-axis is non-zero, the ball will be moving vertically. Similarly, if the velocity on the y-axis is 0 and on the x-axis is non-zero, the ball will be moving horizontally.
What you can do is that you can generate random velocities as well as set boundaries for the generated velocities for the ball not to be moving too slow or too fast.
The drand48()
function returns non-negative double-precision
floating-point values uniformly distributed between [0.0, 1.0].
Suppose I want my generated velocity to be in [3, 5]. I'd suggest one of two options:
- To use
rand()
(i.e., a similar function to drand48()
except that it generates a pseudo-random int
in [0, 2147483647]).
- To use
drand48()
as the pset specification page suggests.
Mathematically, the remainder of dividing an integer a over an integer b is in [0, (b - 1)].
Using rand()
:
The following expression should generate a value in [3, 5]
3 + rand() % [5 - (3 - 1)]
Or more generally, to generate a value in [min, max], you should use a formula like
min + rand() % [max - (min - 1)]
Using drand48()
:
Because drand48() * 10
generates a value in [0, 10], the following expression should generate a value in [3, 5]
3 + (drand48() * 10) % [5 - (3 - 1)]
Or more generally, to generate a value in [min, max], you should use a formula like
min + (drand48() * 10) % [max - (min - 1)]