Created
March 6, 2025 15:05
-
-
Save pitt500/b3f99032e65efbed74cb2dac1ce91dbb to your computer and use it in GitHub Desktop.
CustomRandomNumberGenerator created for my video https://youtu.be/Z4NeyjUTqgg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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