I can’t find a way to use random crops in Swift 3 Beta 1. I had to write a dumb wrapper function in C:
// ---------------------------------------------- // my_random.h // ---------------------------------------------- #ifndef my_random_h #define my_random_h #include <stdio.h> #endif /* my_random_h */ long next_random(); // ---------------------------------------------- // my_random.c // ---------------------------------------------- #include <stdlib.h> #include "my_random.h" long next_random() { return random(); }
You can use the bridge title to import it into Swift. Then you can call it in Swift as follows:
srandom(42) for _ in 0..<10 { let x = next_random() print(x) }
random better than rand . Read the man pages for a discussion of these two features.
Edit:
The workaround, as @riskter suggested, is to use GameKit:
import GameKit let seed = Data(bytes: [42]) // Use any array of [UInt8] let source = GKARC4RandomSource(seed: seed) for _ in 0..<10 { let x = source.nextInt() print(x) }
source share