com.wallstop-studios.unity-helpers 2.0.0-rc04 → 2.0.0-rc06
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md.meta +7 -7
- package/Editor/AnimationEventEditor.cs.meta +11 -11
- package/Editor.meta +8 -8
- package/LICENSE.md.meta +7 -7
- package/README.md.meta +7 -7
- package/Runtime/Core/Attributes/AnimationEventAttribute.cs.meta +11 -11
- package/Runtime/Core/Attributes/ChildComponentAttribute.cs.meta +11 -11
- package/Runtime/Core/Attributes/KSerializableAttribute.cs.meta +11 -11
- package/Runtime/Core/Attributes/NotNullAttribute.cs.meta +11 -11
- package/Runtime/Core/Attributes/ParentComponent.cs.meta +11 -11
- package/Runtime/Core/Attributes/ReadOnlyAttribute.cs.meta +11 -11
- package/Runtime/Core/Attributes/RelationalComponentExtensions.cs.meta +11 -11
- package/Runtime/Core/DataStructure/Adapters/FastVector2Int.cs.meta +11 -11
- package/Runtime/Core/DataStructure/Adapters/FastVector3Int.cs.meta +11 -11
- package/Runtime/Core/DataStructure/Adapters/KGuid.cs.meta +11 -11
- package/Runtime/Core/DataStructure/Adapters/KVector2.cs.meta +11 -11
- package/Runtime/Core/DataStructure/Adapters.meta +8 -8
- package/Runtime/Core/DataStructure/Circle.cs.meta +11 -11
- package/Runtime/Core/DataStructure/CyclicBuffer.cs.meta +11 -11
- package/Runtime/Core/DataStructure/QuadTree.cs.meta +11 -11
- package/Runtime/Core/DataStructure/StringWrapper.cs.meta +11 -11
- package/Runtime/Core/DataStructure/TimedCache.cs +4 -3
- package/Runtime/Core/DataStructure/TimedCache.cs.meta +11 -11
- package/Runtime/Core/DataStructure.meta +8 -8
- package/Runtime/Core/Extension/AnimatorExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/CircleExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/ColorExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/DictionaryExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/DirectionExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/HashSetExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/IEnumerableExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/IListExtensions.cs +2 -2
- package/Runtime/Core/Extension/IListExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/LoggingExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/RandomExtensions.cs +23 -4
- package/Runtime/Core/Extension/RandomExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/StringExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/UnityExtensions.cs +278 -90
- package/Runtime/Core/Extension/UnityExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension.meta +8 -8
- package/Runtime/Core/Helper/ArrayConverter.cs +39 -0
- package/Runtime/Core/Helper/ArrayConverter.cs.meta +3 -0
- package/Runtime/Core/Helper/Enumerables.cs.meta +11 -11
- package/Runtime/Core/Helper/Geometry.cs.meta +11 -11
- package/Runtime/Core/Helper/Helpers.cs +209 -84
- package/Runtime/Core/Helper/LifetimeHelpers.cs.meta +11 -11
- package/Runtime/Core/Helper/Objects.cs.meta +11 -11
- package/Runtime/Core/Helper/WallMath.cs +85 -22
- package/Runtime/Core/Helper/WallMath.cs.meta +11 -11
- package/Runtime/Core/Helper.meta +8 -8
- package/Runtime/Core/Math/Line.cs.meta +11 -11
- package/Runtime/Core/Math/Parabola.cs.meta +11 -11
- package/Runtime/Core/Math/Range.cs.meta +11 -11
- package/Runtime/Core/Math.meta +8 -8
- package/Runtime/Core/Model/Direction.cs.meta +11 -11
- package/Runtime/Core/Model.meta +8 -8
- package/Runtime/Core/OneOf/FastOneOf.cs.meta +11 -11
- package/Runtime/Core/OneOf/None.cs.meta +11 -11
- package/Runtime/Core/OneOf.meta +8 -8
- package/Runtime/Core/Random/AbstractRandom.cs +34 -20
- package/Runtime/Core/Random/AbstractRandom.cs.meta +11 -11
- package/Runtime/Core/Random/DotNetRandom.cs +52 -0
- package/Runtime/Core/Random/DotNetRandom.cs.meta +3 -0
- package/Runtime/Core/Random/IRandom.cs.meta +11 -11
- package/Runtime/Core/Random/NativePcgRandom.cs.meta +11 -11
- package/Runtime/Core/Random/PRNG.cs +7 -0
- package/Runtime/Core/Random/PRNG.cs.meta +3 -0
- package/Runtime/Core/Random/PcgRandom.cs +23 -22
- package/Runtime/Core/Random/PcgRandom.cs.meta +11 -11
- package/Runtime/Core/Random/RandomState.cs +44 -5
- package/Runtime/Core/Random/RandomState.cs.meta +11 -11
- package/Runtime/Core/Random/SquirrelRandom.cs +13 -10
- package/Runtime/Core/Random/SquirrelRandom.cs.meta +11 -11
- package/Runtime/Core/Random/SystemRandom.cs +31 -16
- package/Runtime/Core/Random/SystemRandom.cs.meta +11 -11
- package/Runtime/Core/Random/ThreadLocalRandom.cs +2 -1
- package/Runtime/Core/Random/ThreadLocalRandom.cs.meta +11 -11
- package/Runtime/Core/Random/UnityRandom.cs +37 -4
- package/Runtime/Core/Random/WyRandom.cs +121 -0
- package/Runtime/Core/Random/WyRandom.cs.meta +3 -0
- package/Runtime/Core/Random/XorShiftRandom.cs +9 -7
- package/Runtime/Core/Random.meta +8 -8
- package/Runtime/Core/Serialization/JsonConverters/Vector2Converter.cs.meta +11 -11
- package/Runtime/Core/Serialization/JsonConverters/Vector3Converter.cs.meta +11 -11
- package/Runtime/Core/Serialization/JsonConverters.meta +8 -8
- package/Runtime/Core/Serialization/Serializer.cs +36 -14
- package/Runtime/Core/Serialization/Serializer.cs.meta +11 -11
- package/Runtime/Core/Serialization.meta +8 -8
- package/Runtime/Core/Threading/SingleThreadedThreadPool.cs.meta +11 -11
- package/Runtime/Core/Threading.meta +8 -8
- package/Runtime/Core.meta +8 -8
- package/Runtime/Protobuf-Net.meta +8 -8
- package/Runtime/Utils/AnimationEventEqualityComparer.cs.meta +11 -11
- package/Runtime/Utils/AnimatorEnumStateMachine.cs.meta +11 -11
- package/Runtime/Utils/Buffers.cs.meta +11 -11
- package/Runtime/Utils/CircleLineRenderer.cs +17 -5
- package/Runtime/Utils/CircleLineRenderer.cs.meta +11 -11
- package/Runtime/Utils/Oscillator.cs.meta +11 -11
- package/Runtime/Utils/SetTextureImportData.cs.meta +11 -11
- package/Runtime/Utils.meta +8 -8
- package/Runtime/WallstopStudios.UnityHelpers.asmdef.meta +7 -7
- package/Runtime.meta +8 -8
- package/Tests/Runtime/DataStructures/SpatialTreeTests.cs +34 -10
- package/Tests/Runtime/Helper/ArrayConverterTests.cs +19 -0
- package/Tests/Runtime/Helper/ArrayConverterTests.cs.meta +3 -0
- package/Tests/Runtime/Helper/WallMathTests.cs +221 -0
- package/Tests/Runtime/Helper/WallMathTests.cs.meta +3 -0
- package/Tests/Runtime/Helper.meta +3 -0
- package/Tests/Runtime/Performance/RandomPerformanceTests.cs +18 -13
- package/Tests/Runtime/Performance/SpatialTreePerformanceTest.cs +47 -34
- package/Tests/Runtime/Random/DotNetRandomTests.cs +9 -0
- package/Tests/Runtime/Random/DotNetRandomTests.cs.meta +3 -0
- package/Tests/Runtime/Random/RandomTestBase.cs +89 -15
- package/Tests/Runtime/Random/WyRandomTests.cs +9 -0
- package/Tests/Runtime/Random/WyRandomTests.cs.meta +3 -0
- package/Tests/Runtime/Serialization/JsonSerializationTest.cs +24 -11
- package/Tests/Runtime/Utils/SpriteRendererMetadataTests.cs +21 -17
- package/Tests.meta +8 -8
- package/Third Party Notices.md.meta +7 -7
- package/package.json +1 -1
- package/package.json.meta +7 -7
|
@@ -9,21 +9,20 @@
|
|
|
9
9
|
/// </summary>
|
|
10
10
|
[Serializable]
|
|
11
11
|
[DataContract]
|
|
12
|
-
public sealed class PcgRandom
|
|
12
|
+
public sealed class PcgRandom
|
|
13
|
+
: AbstractRandom,
|
|
14
|
+
IEquatable<PcgRandom>,
|
|
15
|
+
IComparable,
|
|
16
|
+
IComparable<PcgRandom>
|
|
13
17
|
{
|
|
14
18
|
public static IRandom Instance => ThreadLocalRandom<PcgRandom>.Instance;
|
|
15
19
|
|
|
16
|
-
[JsonInclude]
|
|
17
|
-
[JsonPropertyName("Increment")]
|
|
18
|
-
[DataMember(Name = "Increment")]
|
|
19
20
|
internal readonly ulong _increment;
|
|
20
21
|
|
|
21
|
-
[JsonInclude]
|
|
22
|
-
[JsonPropertyName("State")]
|
|
23
|
-
[DataMember(Name = "State")]
|
|
24
22
|
internal ulong _state;
|
|
25
23
|
|
|
26
|
-
public PcgRandom()
|
|
24
|
+
public PcgRandom()
|
|
25
|
+
: this(Guid.NewGuid()) { }
|
|
27
26
|
|
|
28
27
|
public PcgRandom(Guid guid)
|
|
29
28
|
{
|
|
@@ -32,14 +31,14 @@
|
|
|
32
31
|
_increment = BitConverter.ToUInt64(guidArray, sizeof(ulong));
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
|
|
34
|
+
[JsonConstructor]
|
|
35
|
+
public PcgRandom(RandomState internalState)
|
|
36
36
|
{
|
|
37
|
-
_state =
|
|
38
|
-
_increment =
|
|
39
|
-
_cachedGaussian =
|
|
37
|
+
_state = internalState.State1;
|
|
38
|
+
_increment = internalState.State2;
|
|
39
|
+
_cachedGaussian = internalState.Gaussian;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
[JsonConstructor]
|
|
43
42
|
public PcgRandom(ulong increment, ulong state)
|
|
44
43
|
{
|
|
45
44
|
_increment = increment;
|
|
@@ -76,7 +75,14 @@
|
|
|
76
75
|
}
|
|
77
76
|
|
|
78
77
|
// ReSharper disable once CompareOfFloatsByEqualityOperator
|
|
79
|
-
return _increment == other._increment
|
|
78
|
+
return _increment == other._increment
|
|
79
|
+
&& _state == other._state
|
|
80
|
+
&& _cachedGaussian == other._cachedGaussian;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public override bool Equals(object obj)
|
|
84
|
+
{
|
|
85
|
+
return Equals(obj as PcgRandom);
|
|
80
86
|
}
|
|
81
87
|
|
|
82
88
|
public int CompareTo(PcgRandom other)
|
|
@@ -120,9 +126,9 @@
|
|
|
120
126
|
return _cachedGaussian.Value.CompareTo(other._cachedGaussian.Value);
|
|
121
127
|
}
|
|
122
128
|
|
|
123
|
-
public
|
|
129
|
+
public int CompareTo(object obj)
|
|
124
130
|
{
|
|
125
|
-
return
|
|
131
|
+
return CompareTo(obj as PcgRandom);
|
|
126
132
|
}
|
|
127
133
|
|
|
128
134
|
public override int GetHashCode()
|
|
@@ -135,14 +141,9 @@
|
|
|
135
141
|
return $"{{\"Increment\": {_increment}, \"State\": {_state}}}";
|
|
136
142
|
}
|
|
137
143
|
|
|
138
|
-
public int CompareTo(object other)
|
|
139
|
-
{
|
|
140
|
-
return CompareTo(other as PcgRandom);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
144
|
public override IRandom Copy()
|
|
144
145
|
{
|
|
145
146
|
return new PcgRandom(InternalState);
|
|
146
147
|
}
|
|
147
148
|
}
|
|
148
|
-
}
|
|
149
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
fileFormatVersion: 2
|
|
2
|
-
guid: 5206bd9e6b6b7a24cb5a88faba0e4777
|
|
3
|
-
MonoImporter:
|
|
4
|
-
externalObjects: {}
|
|
5
|
-
serializedVersion: 2
|
|
6
|
-
defaultReferences: []
|
|
7
|
-
executionOrder: 0
|
|
8
|
-
icon: {instanceID: 0}
|
|
9
|
-
userData:
|
|
10
|
-
assetBundleName:
|
|
11
|
-
assetBundleVariant:
|
|
1
|
+
fileFormatVersion: 2
|
|
2
|
+
guid: 5206bd9e6b6b7a24cb5a88faba0e4777
|
|
3
|
+
MonoImporter:
|
|
4
|
+
externalObjects: {}
|
|
5
|
+
serializedVersion: 2
|
|
6
|
+
defaultReferences: []
|
|
7
|
+
executionOrder: 0
|
|
8
|
+
icon: {instanceID: 0}
|
|
9
|
+
userData:
|
|
10
|
+
assetBundleName:
|
|
11
|
+
assetBundleVariant:
|
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Random
|
|
2
2
|
{
|
|
3
3
|
using System;
|
|
4
|
+
using System.Linq;
|
|
5
|
+
using System.Runtime.Serialization;
|
|
6
|
+
using System.Text.Json.Serialization;
|
|
4
7
|
using Extension;
|
|
5
8
|
using Helper;
|
|
6
9
|
using ProtoBuf;
|
|
7
10
|
|
|
8
11
|
[Serializable]
|
|
12
|
+
[DataContract]
|
|
9
13
|
[ProtoContract]
|
|
10
14
|
public struct RandomState : IEquatable<RandomState>
|
|
11
15
|
{
|
|
12
16
|
public ulong State1 => _state1;
|
|
17
|
+
|
|
13
18
|
public ulong State2 => _state2;
|
|
14
19
|
|
|
15
20
|
public double? Gaussian
|
|
@@ -25,8 +30,11 @@
|
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
32
|
|
|
33
|
+
public byte[] Payload => _payload;
|
|
34
|
+
|
|
28
35
|
[ProtoMember(1)]
|
|
29
36
|
private ulong _state1;
|
|
37
|
+
|
|
30
38
|
[ProtoMember(2)]
|
|
31
39
|
private ulong _state2;
|
|
32
40
|
|
|
@@ -36,15 +44,31 @@
|
|
|
36
44
|
[ProtoMember(4)]
|
|
37
45
|
private double _gaussian;
|
|
38
46
|
|
|
47
|
+
[ProtoMember(5)]
|
|
48
|
+
private byte[] _payload;
|
|
49
|
+
|
|
39
50
|
private int _hashCode;
|
|
40
51
|
|
|
41
|
-
|
|
52
|
+
[JsonConstructor]
|
|
53
|
+
public RandomState(
|
|
54
|
+
ulong state1,
|
|
55
|
+
ulong state2 = 0,
|
|
56
|
+
double? gaussian = null,
|
|
57
|
+
byte[] payload = null
|
|
58
|
+
)
|
|
42
59
|
{
|
|
43
60
|
_state1 = state1;
|
|
44
61
|
_state2 = state2;
|
|
45
62
|
_hasGaussian = gaussian.HasValue;
|
|
46
63
|
_gaussian = gaussian ?? 0;
|
|
47
|
-
|
|
64
|
+
_payload = payload?.ToArray();
|
|
65
|
+
_hashCode = Objects.ValueTypeHashCode(
|
|
66
|
+
state1,
|
|
67
|
+
state2,
|
|
68
|
+
_hasGaussian,
|
|
69
|
+
_gaussian,
|
|
70
|
+
_payload != null
|
|
71
|
+
);
|
|
48
72
|
}
|
|
49
73
|
|
|
50
74
|
public RandomState(Guid guid)
|
|
@@ -54,7 +78,14 @@
|
|
|
54
78
|
_state2 = BitConverter.ToUInt64(guidBytes, sizeof(ulong));
|
|
55
79
|
_hasGaussian = false;
|
|
56
80
|
_gaussian = 0;
|
|
57
|
-
|
|
81
|
+
_payload = null;
|
|
82
|
+
_hashCode = Objects.ValueTypeHashCode(
|
|
83
|
+
_state1,
|
|
84
|
+
_state2,
|
|
85
|
+
_hasGaussian,
|
|
86
|
+
_gaussian,
|
|
87
|
+
_payload != null
|
|
88
|
+
);
|
|
58
89
|
}
|
|
59
90
|
|
|
60
91
|
[ProtoAfterDeserialization]
|
|
@@ -71,14 +102,22 @@
|
|
|
71
102
|
public bool Equals(RandomState other)
|
|
72
103
|
{
|
|
73
104
|
// ReSharper disable once CompareOfFloatsByEqualityOperator
|
|
74
|
-
return _state1 == other._state1
|
|
105
|
+
return _state1 == other._state1
|
|
106
|
+
&& _state2 == other._state2
|
|
107
|
+
&& _hasGaussian == other._hasGaussian
|
|
108
|
+
&& (!_hasGaussian || _gaussian == other._gaussian);
|
|
75
109
|
}
|
|
76
110
|
|
|
77
111
|
public override int GetHashCode()
|
|
78
112
|
{
|
|
79
113
|
if (_hashCode == 0)
|
|
80
114
|
{
|
|
81
|
-
return _hashCode = Objects.ValueTypeHashCode(
|
|
115
|
+
return _hashCode = Objects.ValueTypeHashCode(
|
|
116
|
+
_state1,
|
|
117
|
+
_state2,
|
|
118
|
+
_hasGaussian,
|
|
119
|
+
_gaussian
|
|
120
|
+
);
|
|
82
121
|
}
|
|
83
122
|
|
|
84
123
|
return _hashCode;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
fileFormatVersion: 2
|
|
2
|
-
guid: 41d80559b2118924ba94e768d005a4e6
|
|
3
|
-
MonoImporter:
|
|
4
|
-
externalObjects: {}
|
|
5
|
-
serializedVersion: 2
|
|
6
|
-
defaultReferences: []
|
|
7
|
-
executionOrder: 0
|
|
8
|
-
icon: {instanceID: 0}
|
|
9
|
-
userData:
|
|
10
|
-
assetBundleName:
|
|
11
|
-
assetBundleVariant:
|
|
1
|
+
fileFormatVersion: 2
|
|
2
|
+
guid: 41d80559b2118924ba94e768d005a4e6
|
|
3
|
+
MonoImporter:
|
|
4
|
+
externalObjects: {}
|
|
5
|
+
serializedVersion: 2
|
|
6
|
+
defaultReferences: []
|
|
7
|
+
executionOrder: 0
|
|
8
|
+
icon: {instanceID: 0}
|
|
9
|
+
userData:
|
|
10
|
+
assetBundleName:
|
|
11
|
+
assetBundleVariant:
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Random
|
|
2
2
|
{
|
|
3
3
|
using System;
|
|
4
|
+
using System.Runtime.Serialization;
|
|
5
|
+
using System.Text.Json.Serialization;
|
|
4
6
|
|
|
5
7
|
// https://youtu.be/LWFzPP8ZbdU?t=2673
|
|
8
|
+
[DataContract]
|
|
9
|
+
[Serializable]
|
|
6
10
|
public sealed class SquirrelRandom : AbstractRandom
|
|
7
11
|
{
|
|
8
12
|
private const uint BitNoise1 = 0xB5297A4D;
|
|
@@ -10,28 +14,27 @@
|
|
|
10
14
|
private const uint BitNoise3 = 0x1B56C4E9;
|
|
11
15
|
private const int LargePrime = 198491317;
|
|
12
16
|
|
|
13
|
-
public static readonly SquirrelRandom Instance =
|
|
17
|
+
public static readonly SquirrelRandom Instance = ThreadLocalRandom<SquirrelRandom>.Instance;
|
|
18
|
+
|
|
19
|
+
public override RandomState InternalState => new(_position, gaussian: _cachedGaussian);
|
|
14
20
|
|
|
15
21
|
private uint _position;
|
|
16
22
|
|
|
17
|
-
public SquirrelRandom()
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
23
|
+
public SquirrelRandom()
|
|
24
|
+
: this(Guid.NewGuid().GetHashCode()) { }
|
|
21
25
|
|
|
22
26
|
public SquirrelRandom(int seed)
|
|
23
27
|
{
|
|
24
28
|
_position = unchecked((uint)seed);
|
|
25
29
|
}
|
|
26
30
|
|
|
27
|
-
|
|
31
|
+
[JsonConstructor]
|
|
32
|
+
public SquirrelRandom(RandomState internalState)
|
|
28
33
|
{
|
|
29
|
-
_position = unchecked((uint)
|
|
30
|
-
_cachedGaussian =
|
|
34
|
+
_position = unchecked((uint)internalState.State1);
|
|
35
|
+
_cachedGaussian = internalState.Gaussian;
|
|
31
36
|
}
|
|
32
37
|
|
|
33
|
-
public override RandomState InternalState => new(_position, gaussian: _cachedGaussian);
|
|
34
|
-
|
|
35
38
|
public override uint NextUint()
|
|
36
39
|
{
|
|
37
40
|
return _position = NextUintInternal(_position);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
fileFormatVersion: 2
|
|
2
|
-
guid: 3f2a41ab545b08e4b9bd2a8025bf839a
|
|
3
|
-
MonoImporter:
|
|
4
|
-
externalObjects: {}
|
|
5
|
-
serializedVersion: 2
|
|
6
|
-
defaultReferences: []
|
|
7
|
-
executionOrder: 0
|
|
8
|
-
icon: {instanceID: 0}
|
|
9
|
-
userData:
|
|
10
|
-
assetBundleName:
|
|
11
|
-
assetBundleVariant:
|
|
1
|
+
fileFormatVersion: 2
|
|
2
|
+
guid: 3f2a41ab545b08e4b9bd2a8025bf839a
|
|
3
|
+
MonoImporter:
|
|
4
|
+
externalObjects: {}
|
|
5
|
+
serializedVersion: 2
|
|
6
|
+
defaultReferences: []
|
|
7
|
+
executionOrder: 0
|
|
8
|
+
icon: {instanceID: 0}
|
|
9
|
+
userData:
|
|
10
|
+
assetBundleName:
|
|
11
|
+
assetBundleVariant:
|
|
@@ -1,25 +1,38 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Random
|
|
2
2
|
{
|
|
3
3
|
using System;
|
|
4
|
+
using System.Runtime.Serialization;
|
|
5
|
+
using System.Text.Json.Serialization;
|
|
6
|
+
using Helper;
|
|
4
7
|
|
|
5
8
|
/// <summary>
|
|
6
9
|
/// Implementation dependent upon .Net's Random class.
|
|
7
10
|
/// </summary>
|
|
11
|
+
[Serializable]
|
|
12
|
+
[DataContract]
|
|
8
13
|
public sealed class SystemRandom : AbstractRandom
|
|
9
14
|
{
|
|
15
|
+
public static IRandom Instance => ThreadLocalRandom<SystemRandom>.Instance;
|
|
16
|
+
|
|
17
|
+
public override RandomState InternalState =>
|
|
18
|
+
new(
|
|
19
|
+
unchecked((ulong)inext),
|
|
20
|
+
unchecked((ulong)inextp),
|
|
21
|
+
_cachedGaussian,
|
|
22
|
+
ArrayConverter.IntArrayToByteArrayBlockCopy(SeedArray)
|
|
23
|
+
);
|
|
24
|
+
|
|
10
25
|
/*
|
|
11
26
|
Copied from Random.cs source. Apparently it isn't guaranteed to be the
|
|
12
|
-
same across platforms
|
|
27
|
+
same across platforms, a fact which defeats the purpose of these serializable
|
|
28
|
+
randoms.
|
|
13
29
|
*/
|
|
14
30
|
private int inext;
|
|
15
31
|
private int inextp;
|
|
16
32
|
private readonly int[] SeedArray = new int[56];
|
|
17
33
|
|
|
18
|
-
public
|
|
19
|
-
|
|
20
|
-
public SystemRandom() : this (Environment.TickCount)
|
|
21
|
-
{
|
|
22
|
-
}
|
|
34
|
+
public SystemRandom()
|
|
35
|
+
: this(Guid.NewGuid().GetHashCode()) { }
|
|
23
36
|
|
|
24
37
|
public SystemRandom(int seed)
|
|
25
38
|
{
|
|
@@ -48,15 +61,18 @@
|
|
|
48
61
|
this.inextp = 21;
|
|
49
62
|
}
|
|
50
63
|
|
|
51
|
-
|
|
64
|
+
[JsonConstructor]
|
|
65
|
+
public SystemRandom(RandomState internalState)
|
|
52
66
|
{
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
67
|
+
unchecked
|
|
68
|
+
{
|
|
69
|
+
inext = (int)internalState.State1;
|
|
70
|
+
inextp = (int)internalState.State2;
|
|
71
|
+
}
|
|
72
|
+
_cachedGaussian = internalState.Gaussian;
|
|
73
|
+
SeedArray = ArrayConverter.ByteArrayToIntArrayBlockCopy(internalState.Payload);
|
|
56
74
|
}
|
|
57
75
|
|
|
58
|
-
public override RandomState InternalState => new(unchecked((ulong)inext), unchecked((ulong)inextp), _cachedGaussian);
|
|
59
|
-
|
|
60
76
|
public override uint NextUint()
|
|
61
77
|
{
|
|
62
78
|
int inext = this.inext;
|
|
@@ -75,7 +91,7 @@
|
|
|
75
91
|
this.SeedArray[index1] = num;
|
|
76
92
|
this.inext = index1;
|
|
77
93
|
this.inextp = index2;
|
|
78
|
-
return unchecked((uint)
|
|
94
|
+
return unchecked((uint)num);
|
|
79
95
|
}
|
|
80
96
|
|
|
81
97
|
public override double NextDouble()
|
|
@@ -84,15 +100,14 @@
|
|
|
84
100
|
do
|
|
85
101
|
{
|
|
86
102
|
generated = unchecked((int)NextUint()) * 4.6566128752458E-10;
|
|
87
|
-
}
|
|
88
|
-
while (generated < 0 || 1 <= generated);
|
|
103
|
+
} while (generated < 0 || 1 <= generated);
|
|
89
104
|
|
|
90
105
|
return generated;
|
|
91
106
|
}
|
|
92
107
|
|
|
93
108
|
public override float NextFloat()
|
|
94
109
|
{
|
|
95
|
-
return (float)
|
|
110
|
+
return (float)NextDouble();
|
|
96
111
|
}
|
|
97
112
|
|
|
98
113
|
public override IRandom Copy()
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
fileFormatVersion: 2
|
|
2
|
-
guid: d00769758e05b2f478ea85ce53650d44
|
|
3
|
-
MonoImporter:
|
|
4
|
-
externalObjects: {}
|
|
5
|
-
serializedVersion: 2
|
|
6
|
-
defaultReferences: []
|
|
7
|
-
executionOrder: 0
|
|
8
|
-
icon: {instanceID: 0}
|
|
9
|
-
userData:
|
|
10
|
-
assetBundleName:
|
|
11
|
-
assetBundleVariant:
|
|
1
|
+
fileFormatVersion: 2
|
|
2
|
+
guid: d00769758e05b2f478ea85ce53650d44
|
|
3
|
+
MonoImporter:
|
|
4
|
+
externalObjects: {}
|
|
5
|
+
serializedVersion: 2
|
|
6
|
+
defaultReferences: []
|
|
7
|
+
executionOrder: 0
|
|
8
|
+
icon: {instanceID: 0}
|
|
9
|
+
userData:
|
|
10
|
+
assetBundleName:
|
|
11
|
+
assetBundleVariant:
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
fileFormatVersion: 2
|
|
2
|
-
guid: 0d9f5b15009b2ff48ba3d66fd8a7fc53
|
|
3
|
-
MonoImporter:
|
|
4
|
-
externalObjects: {}
|
|
5
|
-
serializedVersion: 2
|
|
6
|
-
defaultReferences: []
|
|
7
|
-
executionOrder: 0
|
|
8
|
-
icon: {instanceID: 0}
|
|
9
|
-
userData:
|
|
10
|
-
assetBundleName:
|
|
11
|
-
assetBundleVariant:
|
|
1
|
+
fileFormatVersion: 2
|
|
2
|
+
guid: 0d9f5b15009b2ff48ba3d66fd8a7fc53
|
|
3
|
+
MonoImporter:
|
|
4
|
+
externalObjects: {}
|
|
5
|
+
serializedVersion: 2
|
|
6
|
+
defaultReferences: []
|
|
7
|
+
executionOrder: 0
|
|
8
|
+
icon: {instanceID: 0}
|
|
9
|
+
userData:
|
|
10
|
+
assetBundleName:
|
|
11
|
+
assetBundleVariant:
|
|
@@ -1,16 +1,49 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Random
|
|
2
2
|
{
|
|
3
3
|
using System;
|
|
4
|
+
using System.Runtime.Serialization;
|
|
5
|
+
using System.Text.Json.Serialization;
|
|
4
6
|
|
|
7
|
+
[Serializable]
|
|
8
|
+
[DataContract]
|
|
5
9
|
public sealed class UnityRandom : AbstractRandom
|
|
6
10
|
{
|
|
7
|
-
public static readonly UnityRandom Instance = new
|
|
11
|
+
public static readonly UnityRandom Instance = new();
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
public override RandomState InternalState
|
|
10
14
|
{
|
|
15
|
+
get
|
|
16
|
+
{
|
|
17
|
+
unchecked
|
|
18
|
+
{
|
|
19
|
+
return new RandomState(
|
|
20
|
+
(ulong)(_seed ?? 0),
|
|
21
|
+
gaussian: _seed != null ? 0.0 : null
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private readonly int? _seed;
|
|
28
|
+
|
|
29
|
+
public UnityRandom(int? seed = null)
|
|
30
|
+
{
|
|
31
|
+
if (seed != null)
|
|
32
|
+
{
|
|
33
|
+
_seed = seed.Value;
|
|
34
|
+
UnityEngine.Random.InitState(seed.Value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
[JsonConstructor]
|
|
39
|
+
public UnityRandom(RandomState internalState)
|
|
40
|
+
{
|
|
41
|
+
unchecked
|
|
42
|
+
{
|
|
43
|
+
_seed = internalState.Gaussian != null ? (int)internalState.State1 : null;
|
|
44
|
+
}
|
|
11
45
|
}
|
|
12
46
|
|
|
13
|
-
public override RandomState InternalState => throw new NotSupportedException("Unity Random does not expose its internal state");
|
|
14
47
|
public override uint NextUint()
|
|
15
48
|
{
|
|
16
49
|
return unchecked((uint)UnityEngine.Random.Range(int.MinValue, int.MaxValue));
|
|
@@ -18,7 +51,7 @@
|
|
|
18
51
|
|
|
19
52
|
public override IRandom Copy()
|
|
20
53
|
{
|
|
21
|
-
|
|
54
|
+
return new UnityRandom(_seed);
|
|
22
55
|
}
|
|
23
56
|
}
|
|
24
57
|
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
namespace UnityHelpers.Core.Random
|
|
2
|
+
{
|
|
3
|
+
using System;
|
|
4
|
+
using System.Diagnostics.CodeAnalysis;
|
|
5
|
+
using System.Runtime.CompilerServices;
|
|
6
|
+
using System.Runtime.Serialization;
|
|
7
|
+
using System.Text.Json.Serialization;
|
|
8
|
+
|
|
9
|
+
// https://github.com/cocowalla/wyhash-dotnet/blob/master/src/WyHash/WyRng.cs
|
|
10
|
+
[Serializable]
|
|
11
|
+
[DataContract]
|
|
12
|
+
public sealed class WyRandom : AbstractRandom
|
|
13
|
+
{
|
|
14
|
+
private const ulong Prime0 = 0xa0761d6478bd642f;
|
|
15
|
+
private const ulong Prime1 = 0xe7037ed1a0b428db;
|
|
16
|
+
|
|
17
|
+
public static IRandom Instance => ThreadLocalRandom<WyRandom>.Instance;
|
|
18
|
+
|
|
19
|
+
public override RandomState InternalState => new RandomState(_state);
|
|
20
|
+
|
|
21
|
+
private ulong _state;
|
|
22
|
+
|
|
23
|
+
public WyRandom()
|
|
24
|
+
: this(Guid.NewGuid()) { }
|
|
25
|
+
|
|
26
|
+
public WyRandom(Guid guid)
|
|
27
|
+
{
|
|
28
|
+
byte[] guidArray = guid.ToByteArray();
|
|
29
|
+
_state = BitConverter.ToUInt64(guidArray, 0);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
[JsonConstructor]
|
|
33
|
+
public WyRandom(RandomState internalState)
|
|
34
|
+
{
|
|
35
|
+
_state = internalState.State1;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public WyRandom(ulong state)
|
|
39
|
+
{
|
|
40
|
+
_state = state;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public override uint NextUint()
|
|
44
|
+
{
|
|
45
|
+
unchecked
|
|
46
|
+
{
|
|
47
|
+
_state += Prime0;
|
|
48
|
+
return (uint)Mum(_state ^ Prime1, _state);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// <summary>
|
|
53
|
+
/// Perform a MUM (MUltiply and Mix) operation. Multiplies 2 unsigned 64-bit integers, then combines the
|
|
54
|
+
/// hi and lo bits of the resulting 128-bit integer using XOR
|
|
55
|
+
/// </summary>
|
|
56
|
+
/// <param name="x">First 64-bit integer</param>
|
|
57
|
+
/// <param name="y">Second 64-bit integer</param>
|
|
58
|
+
/// <returns>Result of the MUM (MUltiply and Mix) operation</returns>
|
|
59
|
+
private static ulong Mum(ulong x, ulong y)
|
|
60
|
+
{
|
|
61
|
+
(ulong hi, ulong lo) = Multiply64(x, y);
|
|
62
|
+
return hi ^ lo;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// <summary>
|
|
66
|
+
/// Multiplies 2 unsigned 64-bit integers, returning the result in 2 ulongs representing the hi and lo bits
|
|
67
|
+
/// of the resulting 128-bit integer
|
|
68
|
+
///
|
|
69
|
+
/// Source: https://stackoverflow.com/a/51587262/25758, but with a faster lo calculation
|
|
70
|
+
/// </summary>
|
|
71
|
+
/// <remarks>
|
|
72
|
+
/// <seealso cref="System.Numerics.BigInteger"/> can perform multiplication on large integers, but it's
|
|
73
|
+
/// comparatively slow, and an equivalent method allocates around 360B/call
|
|
74
|
+
/// </remarks>
|
|
75
|
+
/// <param name="x">First 64-bit integer</param>
|
|
76
|
+
/// <param name="y">Second 64-bit integer</param>
|
|
77
|
+
/// <returns>Product of <paramref name="x"/> and <paramref name="y"/></returns>
|
|
78
|
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
79
|
+
[SuppressMessage("ReSharper", "JoinDeclarationAndInitializer")]
|
|
80
|
+
private static unsafe (ulong Hi, ulong Lo) Multiply64(ulong x, ulong y)
|
|
81
|
+
{
|
|
82
|
+
ulong hi;
|
|
83
|
+
ulong lo;
|
|
84
|
+
|
|
85
|
+
// Use BMI2 intrinsics where available
|
|
86
|
+
#if NETCOREAPP3_0_OR_GREATER
|
|
87
|
+
if (System.Runtime.Intrinsics.X86.Bmi2.X64.IsSupported)
|
|
88
|
+
{
|
|
89
|
+
hi = System.Runtime.Intrinsics.X86.Bmi2.X64.MultiplyNoFlags(x, y, &lo);
|
|
90
|
+
return (hi, lo);
|
|
91
|
+
}
|
|
92
|
+
#endif
|
|
93
|
+
|
|
94
|
+
lo = x * y;
|
|
95
|
+
|
|
96
|
+
ulong x0 = (uint)x;
|
|
97
|
+
ulong x1 = x >> 32;
|
|
98
|
+
|
|
99
|
+
ulong y0 = (uint)y;
|
|
100
|
+
ulong y1 = y >> 32;
|
|
101
|
+
|
|
102
|
+
ulong p11 = x1 * y1;
|
|
103
|
+
ulong p01 = x0 * y1;
|
|
104
|
+
ulong p10 = x1 * y0;
|
|
105
|
+
ulong p00 = x0 * y0;
|
|
106
|
+
|
|
107
|
+
// 64-bit product + two 32-bit values
|
|
108
|
+
ulong middle = p10 + (p00 >> 32) + (uint)p01;
|
|
109
|
+
|
|
110
|
+
// 64-bit product + two 32-bit values
|
|
111
|
+
hi = p11 + (middle >> 32) + (p01 >> 32);
|
|
112
|
+
|
|
113
|
+
return (hi, lo);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public override IRandom Copy()
|
|
117
|
+
{
|
|
118
|
+
return new WyRandom(_state);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|