Here's my delimna: I am writing an application that needs to exactly reproduce the PRNG output from a game that was written in Java that uses the Java random() with a given seed to create all it's initial game 'world' data.
The problem I am facing is that Java's random() and ios Swift native PRNG do not generate the same values when given the exact same seeds.
Here are my test cases: In all cases the same 'seed' is used, and the formula is for a random Integer between 0 and 9.
In Java:
import java.util.Random;
long seed = 987234904;
Random rnd = new Random(seed);
int result = rnd.nextInt(10);
The Java random() 'result' = 0
In ios Swift - using srand48() / drand48():
import UIKit
var seed: Int = 987234904
srand48(seed)
var result = Int(drand48()*10)
The ios Swift drand48() 'result' = 7
In ios Swift - using rand_r():
import UIKit
var seed: UInt32 = 987234904
var result = Int(Float(rand_r(&seed))/Float(INT32_MAX)*10)
The ios Swift rand_r() result = 4
With that in mind - is there a ios Swift|Objective-C|C++ code snippit|library available that is the exact same in functionality and output as the Java's version of random()?
Okay - here is the solve:
I asked the same question in the Apple forums and a nice person by the handle of 'ahltorp' shared a c function that they had been using for the very same thing. It's swift compatible and I have tested it against my simulation and it's a flawless match of data!
Github location of the C function: https://github.com/ahltorp/swiftjavarandom
Swift example to use it
Result = 0 :-)
Link to the apple forum discussion: https://forums.developer.apple.com/thread/5791?q=Swift%20prng