Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iphoneSDKid

macrumors newbie
Original poster
Dec 23, 2009
15
0
Hi-
I need a random number generator that creates numbers between 2 numbers, like 10-20, or 20-30, etc.
Thanks!
 
You should try Google "random number generator" for something like this, but:

I don't know Obj-c, but in C++ it would be:

Code:
nRandomInteger = (rand() % nMax) + nLower;

If nMax = 10 and nLower = 1, you will get a random number between 1 and 10.

Usually you seed by using
Code:
srand((unsigned)time(0));
 
Anywhere prior to using "rand". Or use the OS-X function arc4random as Matthew Yohe posted (I haven't done much OS-X dev - most of the time I've been developing for the "dark side".)

Keep in mind that ObjC is a strict superset of C. Whatever works in C works in ObjC.
 
Keep in mind that ObjC is a strict superset of C. Whatever works in C works in ObjC.

Yep. I was just assuming the OP was learning ObjC. For me (if I ever get around to Mac/iPhone development), I'd probably naturally resist the transition and use C/C++, which I also used for embedded systems. Old habits dying young and all that... ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.