Is this a correct porting of java.util.Random in objectiveC

Posted by dipu on Stack Overflow See other posts from Stack Overflow or by dipu
Published on 2010-05-26T00:10:55Z Indexed on 2010/05/26 13:41 UTC
Read the original article Hit count: 336

I have ported the code inside java.util.Random class in objectivec. I want to have an identical random number generator so that it synchs with the server app running on java. Now is this a safe porting and if not is there a way to mimic AtomicLong as it is found in java? Please see my code below.

static long long multiplier = 0x5DEECE66DL;
static long addend = 0xBL;
static long long mask = (0x1000000000000001L << 48) - 1;


-(void) initWithSeed:(long long) seed1 {
    [self setRandomSeed: 0L];// = new AtomicLong(0L);
    [self setSeed: seed1];
}

-(int) next:(int)bits {
    long long oldseed, nextseed;
    long long seed1 = [self.randomSeed longLongValue]; //AtomicLong
    //do {
        oldseed = seed1;
        nextseed = (oldseed * multiplier + addend) & mask;
    //} while (!seed.compareAndSet(oldseed, nextseed));
    [self setRandomSeed: [NSNumber numberWithLongLong:nextseed]];

    ///int ret = (int)(nextseed >>> (48 - bits));
    int ret = (unsigned int)(nextseed >> (48 - bits));
    return ret;
}
-(void) setSeed:(long long) seed1 {
    seed1 = (seed1 ^ multiplier) & mask;
    [self setRandomSeed: [NSNumber numberWithLongLong:seed1]];
}

© Stack Overflow or respective owner

Related posts about java

Related posts about objective-c