-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUM-7937 Use deterministic random ids
- Loading branch information
Showing
5 changed files
with
117 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
18 changes: 18 additions & 0 deletions
18
DatadogSessionReplay/Sources/Recorder/Utilities/Int64+SessionReplay.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
#if os(iOS) | ||
|
||
import Foundation | ||
|
||
@_spi(Internal) | ||
public extension Int64 { | ||
static func positiveRandom<T>(using generator: inout T) -> Int64 where T: RandomNumberGenerator { | ||
.random(in: 0..<Int64.max, using: &generator) | ||
} | ||
} | ||
|
||
#endif |
42 changes: 42 additions & 0 deletions
42
DatadogSessionReplay/Sources/Recorder/Utilities/Xoshiro.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// http://xoshiro.di.unimi.it | ||
/// by David Blackman and Sebastiano Vigna | ||
internal struct XoshiroRandomNumberGenerator: RandomNumberGenerator { | ||
typealias StateType = (UInt64, UInt64, UInt64, UInt64) | ||
|
||
private var state: StateType = (0, 0, 0, 0) | ||
|
||
internal init(seed: StateType) { | ||
self.state = seed | ||
} | ||
|
||
internal init<T>(seed: T) where T: FixedWidthInteger { | ||
self.state = ( | ||
UInt64(seed), | ||
UInt64(seed), | ||
UInt64(seed), | ||
UInt64(seed) | ||
) | ||
} | ||
|
||
internal mutating func next() -> UInt64 { | ||
// Derived from public domain implementation of xoshiro256**: | ||
let x = state.1 &* 5 | ||
let result = ((x &<< 7) | (x &>> 57)) &* 9 | ||
let t = state.1 &<< 17 | ||
state.2 ^= state.0 | ||
state.3 ^= state.1 | ||
state.1 ^= state.2 | ||
state.0 ^= state.3 | ||
state.2 ^= t | ||
state.3 = (state.3 &<< 45) | (state.3 &>> 19) | ||
return result | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import XCTest | ||
|
||
@_spi(Internal) | ||
@testable import DatadogSessionReplay | ||
@testable import TestUtilities | ||
|
||
class XoshiroTests: XCTestCase { | ||
func testRandomnValues() { | ||
let seed: XoshiroRandomNumberGenerator.StateType = (.mockRandom(), .mockRandom(), .mockRandom(), .mockRandom()) | ||
var g1 = XoshiroRandomNumberGenerator(seed: seed) | ||
|
||
let a = g1.next() | ||
let b = g1.next() | ||
let c = g1.next() | ||
let d = g1.next() | ||
|
||
XCTAssert(a != b && a != c && a != d && b != c && b != d && c != d, "Technically, we *could* get a collision...") | ||
} | ||
|
||
func testDeterministicValues() { | ||
let seed: XoshiroRandomNumberGenerator.StateType = (.mockRandom(), .mockRandom(), .mockRandom(), .mockRandom()) | ||
var g1 = XoshiroRandomNumberGenerator(seed: seed) | ||
var g2 = XoshiroRandomNumberGenerator(seed: seed) | ||
|
||
for _ in 0..<1_000 { | ||
XCTAssert(g1.next() == g2.next()) | ||
} | ||
} | ||
} |