initial commit
This commit is contained in:
30
Libraries/MNIST/Random.swift
Normal file
30
Libraries/MNIST/Random.swift
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright © 2024 Apple Inc.
|
||||
|
||||
import Foundation
|
||||
|
||||
// From https://github.com/apple/swift/blob/cb0fb1ea051631219c0b944b84c78571448d58c2/benchmark/utils/TestsUtils.swift#L254
|
||||
//
|
||||
// This is just a seedable RandomNumberGenerator for shuffle()
|
||||
|
||||
// This is a fixed-increment version of Java 8's SplittableRandom generator.
|
||||
// It is a very fast generator passing BigCrush, with 64 bits of state.
|
||||
// See http://dx.doi.org/10.1145/2714064.2660195 and
|
||||
// http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html
|
||||
//
|
||||
// Derived from public domain C implementation by Sebastiano Vigna
|
||||
// See http://xoshiro.di.unimi.it/splitmix64.c
|
||||
public struct SplitMix64: RandomNumberGenerator {
|
||||
private var state: UInt64
|
||||
|
||||
public init(seed: UInt64) {
|
||||
self.state = seed
|
||||
}
|
||||
|
||||
public mutating func next() -> UInt64 {
|
||||
self.state &+= 0x9e37_79b9_7f4a_7c15
|
||||
var z: UInt64 = self.state
|
||||
z = (z ^ (z &>> 30)) &* 0xbf58_476d_1ce4_e5b9
|
||||
z = (z ^ (z &>> 27)) &* 0x94d0_49bb_1331_11eb
|
||||
return z ^ (z &>> 31)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user