Skip to content

Instantly share code, notes, and snippets.

@narrowtux
Created May 8, 2015 10:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narrowtux/4a1c94351d154544300b to your computer and use it in GitHub Desktop.
Save narrowtux/4a1c94351d154544300b to your computer and use it in GitHub Desktop.
Pseudorandom implementation that is deterministic across the platforms Java and Arduino(AVR)
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);
}
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