• Quick note - the problem with Youtube videos not embedding on the forum appears to have been fixed, thanks to ZiprHead. If you do still see problems let me know.

iPhone developers - a little help please

Zax63

Illuminator
Joined
Oct 26, 2005
Messages
3,096
Location
Philadelphia, PA, USA
The goal is to get a random number between -20 and 20. ball1Velocity is a CGPoint.

Code:
ball1Velocity.x = (arc4random() % 21) - 10;
Produces a positive number up to 20 or 4294967296.

Code:
ball1Velocity.x = (arc4random() % 21);
ball1Velocity.x -= 10;
This seems to work properly.

Am I doing something wrong in the first one? :boggled:
 
Are you using your own library? sorry its been a while since I programmed in C

you have 41 numbers total (-20 to 20 = 41 numbers)

i=(rand%41)-20;

or

i=(int)(41.0 * (rand()/(RAND_MAX+1.0)))-20;


hope that is correct; I dont have a compiler to test
 
I'm not an iPhone developer, but I'd guess that arc4random() returns an unsigned integer. The compiler would use unsigned integer math to evaluate the entire right hand side since nothing is there to force it otherwise. If the "-10" causes it to go negative there is no way to represent the number using UINT, the standard compiler response is to go to UINT_MAX.

I'm assuming that ball1Velocity.x is defined as a signed value - then your working example forces the UINT into a signed variable before it can go negative and then you can safely subtract 10.
 
Last edited:
I'm not an iPhone developer, but I'd guess that arc4random() returns an unsigned integer. The compiler would use unsigned integer math to evaluate the entire right hand side since nothing is there to force it otherwise. If the "-10" causes it to go negative there is no way to represent the number using UINT, the standard compiler response is to go to UINT_MAX.

I'm assuming that ball1Velocity.x is defined as a signed value - then your working example forces the UINT into a signed variable before it can go negative and then you can safely subtract 10.

Yes, you are correct.
Changing the code to
Code:
ball1Velocity.x = (int)(arc4random() % 21) - 10;
works.

Yes, I learn programming by trial and error. :blush:

Thanks!
 
You're welcome.

BTW: How do you think I recognized that hidden gotcha? (Learn From My Fail)
 
The goal is to get a random number between -20 and 20. ball1Velocity is a CGPoint.

Code:
ball1Velocity.x = (arc4random() % 21) - 10;
Produces a positive number up to 20 or 4294967296.

For me, this code produces -10 to 10.


Are you using your own library? sorry its been a while since I programmed in C

you have 41 numbers total (-20 to 20 = 41 numbers)

i=(rand%41)-20;

or

i=(int)(41.0 * (rand()/(RAND_MAX+1.0)))-20;


hope that is correct; I dont have a compiler to test

Yes the below gives me -20 to 20:
Here is my specific code (in case perchance your nslog is wrong):

Code:
int x, y;
for (x = 0;  x < 100; x++) {
   y = (arc4random() %41) - 20;
   NSLog(@"y = %d", y);
}
 
:blush: again. I wanted -10 to 10 don't know why I used 20 in the OP.

I'm doing a little physics simulator as a learning tool. I figure I can keep adding features to try out new techniques and it's kind of fun to watch the balls bounce around the screen. I've got gravity, friction, elasticity and next I'm adding a magnet. Doing it for the iPad at the moment as I've got one on order. One future task will be to make it run on either iPad or iPhone.
 

Attachments

  • Magnets_Cap01.gif
    Magnets_Cap01.gif
    4.5 KB · Views: 3

Back
Top Bottom