Verse Library verse

01 Snippet

Implements bitwise operations like XOR and shifts to power a deterministic random number generator.

extracted-snippets/community/snippets/KyRK/fortnite-seedable-random-number-generator/01-snippet.verse

# Source URL: https://dev.epicgames.com/community/snippets/KyRK/fortnite-seedable-random-number-generator
# Local doc:  epic-docs/community/snippets/KyRK/fortnite-seedable-random-number-generator.md
  SeedableRandom<public> := module:
  LeftShift(Value : int, Shift : int)<transacts><decides>:int=
  var Output : int = 0
  for (Index := 0..(31 - Shift)):
  Shifted := RightShift[Value, Index]
  Bit := Mod[Shifted, 2]
  if (Bit = 1):
  set Output += Floor[Pow(2.0, (Index + Shift) * 1.0)]
  return Output
  RightShift(Value : int, Shift : int)<transacts><decides>:int=
  return Floor[(Value * 1.0) / (Pow(2.0, Shift * 1.0))]
  XOR(A : int, B : int)<transacts><decides>:int=
  var Output : int = 0
  var ShiftedA : int = A
  var ShiftedB : int = B
  for (Index := 0..63, ShiftedA <> 0 or ShiftedB <> 0):
  BitA := Mod[ShiftedA, 2]
  BitB := Mod[ShiftedB, 2]
  if ((BitA = 1 and BitB = 0) or (BitA = 0 and BitB = 1)):
  set Output = Output + LeftShift[1, Index]
  set ShiftedA = RightShift[ShiftedA, 1]
  set ShiftedB = RightShift[ShiftedB, 1]
  return Output
  OR(A : int, B : int)<transacts><decides>:int=
  var Output : int = 0
  var ShiftedA : int = A
  var ShiftedB : int = B
  for (Index := 0..63, ShiftedA <> 0 or ShiftedB <> 0):

Sign in free to read the full source 🌴

This Verse Library file is members-only. Create a free account to view the complete source and download the .verse file.

Sign in with Discord

Comments

    Sign in to vote, comment, or suggest an edit. Sign in