-
Notifications
You must be signed in to change notification settings - Fork 9
Implement reproducible random #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
47745d4
Impl reproducible random (xoshiro256++), enhance sequential id genera…
evgTSV f0cbf5c
Add license
evgTSV 9f394d6
Add license
evgTSV a8fb646
Merge remote-tracking branch 'origin/ran-gen' into ran-gen
evgTSV 99a2835
Fix :D
evgTSV 44f9076
Rollback changes depended on seq id gen, fix jump function
evgTSV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // SPDX-FileCopyrightText: 2025 O21 contributors <https://github.com/ForNeVeR/O21> | ||
| // | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| namespace O21.Game.Engine | ||
|
|
||
| open System | ||
| open System.Numerics | ||
| open O21.Game.MemoryHelpers | ||
|
|
||
| /// <summary> | ||
| /// Implementation of xoshiro256++ <br/> | ||
| /// See original algorithm <a href="https://prng.di.unimi.it/xoshiro256plusplus.c">here</a> | ||
| /// </summary> | ||
| type RandomGenerator(seed: int64) = | ||
|
|
||
| let randState: uint64[] = Array.zeroCreate 4 | ||
|
|
||
| let jump = [| | ||
| 0x180ec6d33cfd0abaUL; 0xd5a61266f0c9392cUL | ||
| 0xa9582618e03fc9aaUL; 0x39abdc4529b1661cUL | ||
| |] | ||
|
|
||
| let longJump = [| | ||
| 0x76e15d3efefdcbbfUL; 0xc5004e441c522fb3UL | ||
| 0x77710069854ee241UL; 0x39109bb02acbe635UL | ||
| |] | ||
|
|
||
| /// Use splitmix64 for init (recommended by the authors) | ||
| let initializeState (seed: int64) = | ||
| let mutable x = uint64 seed | ||
| let next() = | ||
| x <- x + 0x9E3779B97F4A7C15UL | ||
| let mutable z = x | ||
| z <- (z ^^^ (z >>> 30)) * 0xBF58476D1CE4E5B9UL | ||
| z <- (z ^^^ (z >>> 27)) * 0x94D049BB133111EBUL | ||
| z ^^^ (z >>> 31) | ||
|
|
||
| for i = 0 to 3 do | ||
| randState[i] <- next() | ||
|
|
||
| let rotl (x: uint64) (k: int) = | ||
| BitOperations.RotateLeft(x, k) | ||
|
|
||
| do initializeState(seed) | ||
|
|
||
| member this.Seed = seed | ||
|
|
||
| member this.Next() : uint64 = | ||
| let result = rotl (randState[0] + randState[3]) 23 + randState[0] | ||
| let t = randState[1] <<< 17 | ||
|
|
||
| randState[2] <- randState[2] ^^^ randState[0] | ||
| randState[3] <- randState[3] ^^^ randState[1] | ||
| randState[1] <- randState[1] ^^^ randState[2] | ||
| randState[0] <- randState[0] ^^^ randState[3] | ||
|
|
||
| randState[2] <- randState[2] ^^^ t | ||
| randState[3] <- rotl randState[3] 45 | ||
|
|
||
| result | ||
|
|
||
| /// [0.0, 1.0) | ||
| member this.NextDouble() = | ||
| float (this.Next() >>> 11) * (1.0 / 9007199254740992.0) | ||
|
|
||
| /// [minValue, maxValue) | ||
| member this.NextDouble(minValue: float, maxValue: float) = | ||
| minValue + (maxValue - minValue) * this.NextDouble() | ||
|
|
||
| /// [0, maxValue) | ||
| member this.Next(?maxValue: int) = | ||
| let maxValue = defaultArg maxValue Int32.MaxValue | ||
| if maxValue <= 0 then invalidArg "maxValue" "Must be positive" | ||
| let threshold = (0x7FFFFFFFUL / uint64 maxValue) * uint64 maxValue | ||
| let rec loop() = | ||
| let r = this.Next() &&& 0x7FFFFFFFUL // 31 bits for positive integers | ||
| if r < threshold then int (r % uint64 maxValue) | ||
| else loop() | ||
| loop() | ||
|
|
||
| /// [minValue, maxValue) | ||
| member this.Next(minValue: int, maxValue: int) = | ||
| if minValue > maxValue then invalidArg "minValue" "minValue cannot be greater than maxValue" | ||
| minValue + this.Next(maxValue - minValue) | ||
|
|
||
| member this.NextBool() = | ||
| (this.Next() &&& 1UL) = 0UL | ||
|
|
||
| member this.NextBool probability = | ||
| if probability <= 0.0 then false | ||
| elif probability >= 1.0 then true | ||
| else this.NextDouble() < probability | ||
|
|
||
| member this.NextBytes(buffer: byte[]) = | ||
| for i in 0 .. 8 .. buffer.Length - 1 do | ||
| let randomValue = this.Next() | ||
| for j in 0 .. min 7 (buffer.Length - i - 1) do | ||
| buffer[i + j] <- byte ((randomValue >>> (j * 8)) &&& 0xFFUL) | ||
|
|
||
| member this.SaveState(buffer: Span<uint64>) = | ||
| if buffer.Length < 4 then | ||
| invalidArg (nameof(buffer)) "State must have exactly 4 elements" | ||
| randState.CopyTo buffer | ||
|
|
||
| member this.LoadState(state: Span<uint64>) = | ||
| if state.Length <> 4 then | ||
| invalidArg (nameof(state)) "State must have exactly 4 elements" | ||
| state.CopyTo randState | ||
|
|
||
| member private this.JumpBase(jumps: uint64[]) = | ||
| let newState = stackalloc<uint64> 4 | ||
| for i in 0 .. 3 do | ||
| for b in 0 .. 63 do | ||
| if (jumps[i] &&& (1UL <<< b)) <> 0UL then | ||
| for j in 0 .. 3 do | ||
| newState[j] <- newState[j] ^^^ randState[j] | ||
| this.Next() |> ignore | ||
|
|
||
| let generator = RandomGenerator(0) | ||
| generator.LoadState(newState) | ||
| generator | ||
|
|
||
| member this.Jump() = | ||
| this.JumpBase(jump) | ||
|
|
||
| member this.LongJump() = | ||
| this.JumpBase(longJump) |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // SPDX-FileCopyrightText: 2025 O21 contributors <https://github.com/ForNeVeR/O21> | ||
| // | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| module O21.Game.MemoryHelpers | ||
|
|
||
| #nowarn 9 | ||
| open System | ||
| open FSharp.NativeInterop | ||
|
|
||
| let inline stackalloc<'T when 'T: unmanaged> (length: int) : Span<'T> = | ||
| let ptr = NativePtr.stackalloc<'T> length | ||
| let span = Span<'T>(NativePtr.toVoidPtr ptr, length) | ||
| span.Clear() | ||
| span |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly I don't understand why these methods were introduced in the
ReproducibleRandomclass to begin with; they are not supposed to be random at all.Let's not touch these and
SequentialIdGeneratorin this PR? I'll open a separate issue to move them out of this type.The
SequentialIdGeneratorwas supposed to be sequential, not random. Moreover, random by its very definition is not unique, and uniqueness is what we need from the identifiers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let's move it another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#319.