Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. public class SequenceToGPU {
  2.  
  3. public static final LCG JAVA_LCG = new LCG(0x5DEECE66DL, 0xBL, 1L << 48);
  4.  
  5. public static void main(String[] args) {
  6. String sequence = "1122221111112011011122211101112201000012201011012211101102211111102222222222";
  7.  
  8. int combination = 0;
  9.  
  10. for(int i = 0; i < sequence.length(); i++) {
  11. int n = Integer.parseInt(String.valueOf(sequence.charAt(i)));
  12.  
  13. if(n == 0) {
  14. if(combination != 0) {
  15. LCG lcg = JAVA_LCG.combine(combination);
  16. System.out.println("seed = (seed * " + lcg.multiplier + "LL + " + lcg.addend + "LL) & " + (lcg.modulo - 1) + "LL;");
  17. combination = 0;
  18. }
  19.  
  20. System.out.println("if(next(&seed, 2) != 0)return;");
  21. } else if(n == 1) {
  22. if(combination != 0) {
  23. LCG lcg = JAVA_LCG.combine(combination);
  24. System.out.println("seed = (seed * " + lcg.multiplier + "LL + " + lcg.addend + "LL) & " + (lcg.modulo - 1) + "LL;");
  25. combination = 0;
  26. }
  27.  
  28. System.out.println("if(next(&seed, 2) == 0)return;");
  29. } else if(n == 2) {
  30. combination++;
  31. }
  32. }
  33.  
  34. }
  35.  
  36. public static class LCG {
  37.  
  38. public final long multiplier;
  39. public final long addend;
  40. public final long modulo;
  41.  
  42. private final boolean hasModulo;
  43. private final boolean canMask;
  44.  
  45. public LCG(long multiplier, long addend) {
  46. this.multiplier = multiplier;
  47. this.addend = addend;
  48. this.modulo = 0;
  49.  
  50. this.hasModulo = false;
  51. this.canMask = true;
  52. }
  53.  
  54. public LCG(long multiplier, long addend, long modulo) {
  55. this.multiplier = multiplier;
  56. this.addend = addend;
  57. this.modulo = modulo;
  58.  
  59. this.hasModulo = true;
  60. this.canMask = (this.modulo & -this.modulo) == this.modulo;
  61. }
  62.  
  63. public long nextSeed(long seed) {
  64. if(!this.hasModulo) {
  65. return seed * this.multiplier + this.addend;
  66. }
  67.  
  68. if(this.canMask) {
  69. return (seed * this.multiplier + this.addend) & (this.modulo - 1);
  70. }
  71.  
  72. return (seed * this.multiplier + this.addend) % this.modulo;
  73. }
  74.  
  75. public LCG combine(long steps) {
  76. long multiplier = 1;
  77. long addend = 0;
  78.  
  79. long intermediateMultiplier = this.multiplier;
  80. long intermediateAddend = this.addend;
  81.  
  82. for(long k = steps; k != 0; k >>>= 1) {
  83. if((k & 1) != 0) {
  84. multiplier *= intermediateMultiplier;
  85. addend = intermediateMultiplier * addend + intermediateAddend;
  86. }
  87.  
  88. intermediateAddend = (intermediateMultiplier + 1) * intermediateAddend;
  89. intermediateMultiplier *= intermediateMultiplier;
  90. }
  91.  
  92. if(this.canMask) {
  93. multiplier &= (this.modulo - 1);
  94. addend &= (this.modulo - 1);
  95. } else {
  96. multiplier %= this.modulo;
  97. addend %= this.modulo;
  98. }
  99.  
  100. return new LCG(multiplier, addend, this.modulo);
  101. }
  102.  
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement