Random Numbers in Objective-C

Here’s the translation of the Go code to Objective-C, formatted in Markdown suitable for Hugo:

Our first program demonstrates generating random numbers. Here’s the full source code:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // For example, arc4random_uniform returns a random uint32_t n,
        // 0 <= n < upperBound
        NSLog(@"%u,%u", arc4random_uniform(100), arc4random_uniform(100));
        
        // drand48() returns a double f,
        // 0.0 <= f < 1.0
        NSLog(@"%f", drand48());
        
        // This can be used to generate random floats in
        // other ranges, for example 5.0 <= f' < 10.0
        NSLog(@"%f,%f", (drand48() * 5) + 5, (drand48() * 5) + 5);
        
        // If you want a known seed, use srand48()
        // to set the seed for drand48()
        srand48(42);
        NSLog(@"%u,%u", arc4random_uniform(100), arc4random_uniform(100));
        
        // Using the same seed will produce the same sequence
        srand48(42);
        NSLog(@"%u,%u", arc4random_uniform(100), arc4random_uniform(100));
    }
    return 0;
}

To run the program, save it as RandomNumbers.m and compile it using:

$ clang -framework Foundation RandomNumbers.m -o RandomNumbers
$ ./RandomNumbers

Some of the generated numbers may be different when you run the sample:

68,56
0.809023
5.840125,6.937056
94,49
94,49

In Objective-C, we use arc4random_uniform() for generating random integers and drand48() for random floating-point numbers. The srand48() function is used to set a seed for drand48(), which allows for reproducible sequences of random numbers.

Note that Objective-C doesn’t have a built-in equivalent to Go’s math/rand/v2 package with its various random number generators. For more complex random number generation needs, you might need to use third-party libraries or implement custom solutions.

For more information on random number generation in Objective-C, refer to the Apple documentation on arc4random and drand48 functions.