Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SequenceToGPU {
- public static final LCG JAVA_LCG = new LCG(0x5DEECE66DL, 0xBL, 1L << 48);
- public static void main(String[] args) {
- String sequence = "1122221111112011011122211101112201000012201011012211101102211111102222222222";
- int combination = 0;
- for(int i = 0; i < sequence.length(); i++) {
- int n = Integer.parseInt(String.valueOf(sequence.charAt(i)));
- if(n == 0) {
- if(combination != 0) {
- LCG lcg = JAVA_LCG.combine(combination);
- System.out.println("seed = (seed * " + lcg.multiplier + "LL + " + lcg.addend + "LL) & " + (lcg.modulo - 1) + "LL;");
- combination = 0;
- }
- System.out.println("if(next(&seed, 2) != 0)return;");
- } else if(n == 1) {
- if(combination != 0) {
- LCG lcg = JAVA_LCG.combine(combination);
- System.out.println("seed = (seed * " + lcg.multiplier + "LL + " + lcg.addend + "LL) & " + (lcg.modulo - 1) + "LL;");
- combination = 0;
- }
- System.out.println("if(next(&seed, 2) == 0)return;");
- } else if(n == 2) {
- combination++;
- }
- }
- }
- public static class LCG {
- public final long multiplier;
- public final long addend;
- public final long modulo;
- private final boolean hasModulo;
- private final boolean canMask;
- public LCG(long multiplier, long addend) {
- this.multiplier = multiplier;
- this.addend = addend;
- this.modulo = 0;
- this.hasModulo = false;
- this.canMask = true;
- }
- public LCG(long multiplier, long addend, long modulo) {
- this.multiplier = multiplier;
- this.addend = addend;
- this.modulo = modulo;
- this.hasModulo = true;
- this.canMask = (this.modulo & -this.modulo) == this.modulo;
- }
- public long nextSeed(long seed) {
- if(!this.hasModulo) {
- return seed * this.multiplier + this.addend;
- }
- if(this.canMask) {
- return (seed * this.multiplier + this.addend) & (this.modulo - 1);
- }
- return (seed * this.multiplier + this.addend) % this.modulo;
- }
- public LCG combine(long steps) {
- long multiplier = 1;
- long addend = 0;
- long intermediateMultiplier = this.multiplier;
- long intermediateAddend = this.addend;
- for(long k = steps; k != 0; k >>>= 1) {
- if((k & 1) != 0) {
- multiplier *= intermediateMultiplier;
- addend = intermediateMultiplier * addend + intermediateAddend;
- }
- intermediateAddend = (intermediateMultiplier + 1) * intermediateAddend;
- intermediateMultiplier *= intermediateMultiplier;
- }
- if(this.canMask) {
- multiplier &= (this.modulo - 1);
- addend &= (this.modulo - 1);
- } else {
- multiplier %= this.modulo;
- addend %= this.modulo;
- }
- return new LCG(multiplier, addend, this.modulo);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement