Skip to content

Instantly share code, notes, and snippets.

@jzelinskie
Created June 4, 2017 06:45
Show Gist options
  • Save jzelinskie/af9a119e697c3c8a0dae3e388d60310b to your computer and use it in GitHub Desktop.
Save jzelinskie/af9a119e697c3c8a0dae3e388d60310b to your computer and use it in GitHub Desktop.
mario kart64 rng in Go
package main
import "fmt"
func SuperMario64RNG(input uint16) uint16 {
if input == 0x560A {
input = 0
}
var s0 uint16 = uint16(uint8(input) << 8)
s0 = s0 ^ input
input = ((s0 & 0xFF) << 8) | ((s0 & 0xFF00) >> 8)
s0 = uint16((uint8(s0) << 1)) ^ input
var s1 uint16 = (s0 >> 1) ^ 0xFF80
if (s0 & 1) == 0 {
if s1 == 0xAA55 {
input = 0
} else {
input = s1 ^ 0x1FF4
}
} else {
input = s1 ^ 0x8180
}
return input
}
func Random(seed uint16) func() uint16 {
return func() uint16 {
seed = SuperMario64RNG(seed)
return seed
}
}
func main() {
fmt.Println(SuperMario64RNG(1))
fmt.Println(SuperMario64RNG(57589))
fmt.Println(SuperMario64RNG(39665))
fmt.Println()
fn := Random(1)
fmt.Println(fn())
fmt.Println(fn())
fmt.Println(fn())
}
// Outputs:
// 57589
// 39665
// 39112
//
// 57589
// 39665
// 39112
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment