Created
May 8, 2015 10:47
-
-
Save narrowtux/4a1c94351d154544300b to your computer and use it in GitHub Desktop.
Pseudorandom implementation that is deterministic across the platforms Java and Arduino(AVR)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uint32_t seed = 1203; // or whatever you like | |
uint32_t next(uint16_t bits) { | |
seed = (seed * 0xECE66DL + 0xBL) & ((1L << 31) - 1); | |
return (uint32_t) (seed >> (32 - bits)); | |
} | |
uint32_t nextInt() { | |
return next(32); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class SimpleRandom extends Random { | |
long seed; | |
public SimpleRandom(long seed) { | |
setSeed(seed); | |
} | |
@Override | |
public void setSeed(long seed) { | |
this.seed = seed & ((1L << 31) - 1); | |
} | |
@Override | |
protected int next(int bits) { | |
seed = (seed * 0xECE66DL + 0xBL) & ((1L << 31) - 1); | |
return (int) (seed >> (32 - bits)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment