SimplyKiwi
SimplyKiwi

Reputation: 12444

Randomizing pre-defined numbers

In my app I would like to randomize set values which I set in #define's. I am looking to use arc4random also. I usually would know how to do this but I have only seen tutorials with very basic things like numbers 0-10!

Any tips/help would be appreciated!

Upvotes: 0

Views: 158

Answers (2)

Stoph
Stoph

Reputation: 723

From the Wikipedia objective C article it looks like you can define macros using #define. From their example:

#define Add(x,y) ( x + y )

int a = 1;
int b = 2;
int c = Add(a,b);
NSLog(@"Add result: %i", c);
// this will output
// Add result: 3

I'm not sure how complex you can get with those, but I would think you'd be able to do something like #define MY_VAL() (arc4random()%100) to get a range of values, or maybe even use AlexTeho's idea within the macro.

Upvotes: 0

AlexTheo
AlexTheo

Reputation: 4184

put all of your numbers to an array after that calculate an random number in range of 0 and sizeof your array. After you can get your randomized value from random place of predefined array and remove this value. Do it again for range 0 sizoef array - 1 and so on.

Upvotes: 1

Related Questions