Skip to content

Instantly share code, notes, and snippets.

@pitt500
Created March 6, 2025 15:05
Show Gist options
  • Save pitt500/b3f99032e65efbed74cb2dac1ce91dbb to your computer and use it in GitHub Desktop.
Save pitt500/b3f99032e65efbed74cb2dac1ce91dbb to your computer and use it in GitHub Desktop.
CustomRandomNumberGenerator created for my video https://youtu.be/Z4NeyjUTqgg
import Foundation
struct CustomRandomNumberGenerator: RandomNumberGenerator {
private var state: UInt64 = 0
private let multiplier: UInt64 = 6364136223846793005
private let increment: UInt64 = 1442695040888963407
private let modulus: UInt64 = UInt64.max // 2^64 - 1
init(seed: UInt64) {
state = seed
}
mutating func next() -> UInt64 {
state = (multiplier &* state &+ increment) % modulus
//print("Random number generated: \(state)")
return state
}
}
var generator = CustomRandomNumberGenerator(seed: 7)
for _ in 1...20 {
print(Bool.random(using: &generator))
}
let value = Int.random(in: 1...10)
let value2 = Int.random(in: 10...1)
let value3 = Int.random(in: Int.min...Int.max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment