I\'m a Java head mainly, and I want a way to generate a pseudo-random number between 0 and 74. In Java I would use the method:
Random.nextInt(74)
I wrote my own random number utility class just so that I would have something that functioned a bit more like Math.random() in Java. It has just two functions, and it's all made in C.
Header file:
//Random.h
void initRandomSeed(long firstSeed);
float nextRandomFloat();
Implementation file:
//Random.m
static unsigned long seed;
void initRandomSeed(long firstSeed)
{
seed = firstSeed;
}
float nextRandomFloat()
{
return (((seed= 1664525*seed + 1013904223)>>16) / (float)0x10000);
}
It's a pretty classic way of generating pseudo-randoms. In my app delegate I call:
#import "Random.h"
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
initRandomSeed( (long) [[NSDate date] timeIntervalSince1970] );
//Do other initialization junk.
}
Then later I just say:
float myRandomNumber = nextRandomFloat() * 74;
Note that this method returns a random number between 0.0f (inclusive) and 1.0f (exclusive).