0

I don't want to have my ball go at a random velocity when it starts, just a random angle with respect to x. The specs say to use drand48, which I tried, but that makes the ball go really slowly or really fast sometimes, and I want my ball to have a constant speed.

Any tips?

3

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:

  1. To use rand() (i.e., a similar function to drand48() except that it generates a pseudo-random int in [0, 2147483647]).
  2. 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)]
9
  • I don't really understand why all this % math is needed. Can't one just use - double x = drand48()*10.0 +2; if i want the range [2,10)?
    – Amrita
    Sep 12 '14 at 12:10
  • @Amrita unfortunately, this wouldn't result in a number within [2, 10]. Rather, this would result in a number in [2, 12]. You may ask about what you don't understand exactly or take the formula for granted!
    – kzidane
    Sep 12 '14 at 14:30
  • If I use the syntax given above, the compiler gives me an error of 'expected expression' - pointing at the square brackets.
    – Amrita
    Sep 12 '14 at 16:27
  • @Amrita the expression above is not valid C syntax. I wanted to show the math point at the first place. Try replacing the brackets with parentheses!
    – kzidane
    Sep 12 '14 at 16:34
  • @Kareem, I've tried what you suggested but either it yells error: invalid operands to binary expression or error: expected expression. I've tried different parenthese combianations. I also hope I can make the ball move to the left sometimes, like staff's example. Apr 8 '15 at 7:31
1

While not exactly correct, a simplified explanation might be:

The Y velocity controls the vertical speed. The X velocity controls the angle.

So in order to maintain a set vertical speed but a random angle, one would randomize just the X velocity using srand48().

1

@user1647 I've just figured out how to move the ball with random angles, left or right with (- n, + n) angle like the staff's breakout.

Hints:

  1. Remember that drand48() gives you a random number between 0.0 and 1.0. So if you want it to move the X between - 1 to 1, you should see numbers from 0 to 1 a bit differently. Use a bit of maths.

  2. In the pset, the staff also mentioned: add some constant to it and/or multiply it by some constant!

1
  • (2drand48 - 1) for -1 to +1 but how to arrive at general formula on my own?
    – taichouvik
    Jun 29 '16 at 11:17
0

To get a range of [-n , n] from drand48() which has initial range of [1 , 0] do the following:

rane = [min , max]

drand48() = [0 , 1]

But we want= [-n , n]

can we add/multiply something to 0 to make it equal to -n? Yes.

= 0 + (-n)

= -n

But what about max?

= 1 + (-n)

!= n (oops!)

But can something be multiplied to 1? Yes.

= (2n)*1 + (-n)

= n

generalising c = p*d + q

min(c) = p*min(d) + q

max(c) = p*max(d) + q

where d = drand48() ,p = 2n , q = (-n)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .