Skip to content

Commit

Permalink
fix: fix MeteorId XorShift overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
huangxiaohu committed Feb 17, 2023
1 parent d9ad607 commit 93e6c17
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ public synchronized long nexId() {
throw new IllegalStateException("Seconds overflow. The max second is " + maxSecond);
}
}
return second << timeShift | nodeId << nodeShift | seqNum << seqShift | randNum;

long l = second << timeShift | nodeId << nodeShift | seqNum << seqShift | randNum;
if (l < 0) {
System.out.println("Seconds overflow");
}
return l;
}

public void setNodeId(long nodeId) {
Expand All @@ -118,10 +121,12 @@ public void setNodeId(long nodeId) {
* @return the random number
*/
private static long rand(Node node) {
node.seed ^= node.seed << 7;
node.seed ^= node.seed >> 9;
node.seed ^= node.seed << 8;
return node.seed % MeteorId.maxRand;
long x = node.seed;
x ^= x << 13;
x ^= x << 17;
x ^= x << 5;
node.seed = x % MeteorId.maxRand;
return node.seed;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,4 @@ private static int getProcessPiece() {
return processPiece;
}
// ----------------------------------------------------------------------------------------- Private method end
}
}

0 comments on commit 93e6c17

Please sign in to comment.