Skip to main content
1 of 2
Mark Jeronimus
  • 7.2k
  • 6
  • 18
  • 23

Java - 52

int m=0;int rand7(){return(m=m*5&-1>>>1|rand5())%7;}

Distribution test: [1000915, 999689, 999169, 998227, 1001653, 1000419, 999928]

Algorithm:

  • Keep a global variable
  • multiply by 5, so there get 5 places free at the least significant end
  • Truncate the sign bit to make it positive (not necessary if unsigned numbers were supported)
  • Modulo 7 is the answer

> The numbers are not mutually uncorrelated anymore.

Mark Jeronimus
  • 7.2k
  • 6
  • 18
  • 23