Try the search, it's linked to some great forums

Monday, June 27, 2011

random number generator

Use this.  y is an integer random number. x is the modulo (remainder of y/100) divided by 1000. Or a number between 0.000 and 0.100.

    int y = (random());      
    float x = (y % 100) / 1000.0;
    NSLog(@"random() is %d", y);
    NSLog(@"modulo is %2.3f", x);

Use this, to create a number between -1.000 to +1.000;

    int y = (random() % 2000) - 1000;       
    float x = y / 1000.0;
    NSLog(@"random() is %d", y);
    NSLog(@"modulo is %2.3f", x);


Apparently, this is not truly random, because the number series is always identical.  Good enough for many things.
Other functions to look at are arc4random() or srand(time(NULL)); which requires seeding.