com.wallstop-studios.unity-helpers 2.0.0-rc04 → 2.0.0-rc05
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.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.meta +11 -11
- package/Runtime/Core/Extension/LoggingExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/RandomExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/StringExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension/UnityExtensions.cs.meta +11 -11
- package/Runtime/Core/Extension.meta +8 -8
- package/Runtime/Core/Helper/Enumerables.cs.meta +11 -11
- package/Runtime/Core/Helper/Geometry.cs.meta +11 -11
- package/Runtime/Core/Helper/LifetimeHelpers.cs.meta +11 -11
- package/Runtime/Core/Helper/Objects.cs.meta +11 -11
- 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.meta +11 -11
- package/Runtime/Core/Random/DotNetRandom.cs +54 -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/PcgRandom.cs +25 -22
- package/Runtime/Core/Random/PcgRandom.cs.meta +11 -11
- package/Runtime/Core/Random/RandomState.cs +13 -2
- package/Runtime/Core/Random/RandomState.cs.meta +11 -11
- package/Runtime/Core/Random/SquirrelRandom.cs +12 -7
- package/Runtime/Core/Random/SquirrelRandom.cs.meta +11 -11
- package/Runtime/Core/Random/SystemRandom.cs +15 -9
- package/Runtime/Core/Random/SystemRandom.cs.meta +11 -11
- package/Runtime/Core/Random/ThreadLocalRandom.cs.meta +11 -11
- package/Runtime/Core/Random/UnityRandom.cs +39 -4
- package/Runtime/Core/Random/WyRandom.cs +123 -0
- package/Runtime/Core/Random/WyRandom.cs.meta +3 -0
- package/Runtime/Core/Random/XorShiftRandom.cs +8 -4
- 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.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.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/Performance/RandomPerformanceTests.cs +18 -13
- package/Tests/Runtime/Random/DotNetRandomTests.cs +9 -0
- package/Tests/Runtime/Random/DotNetRandomTests.cs.meta +3 -0
- package/Tests/Runtime/Random/RandomTestBase.cs +69 -15
- package/Tests/Runtime/Random/WyRandomTests.cs +9 -0
- package/Tests/Runtime/Random/WyRandomTests.cs.meta +3 -0
- 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,22 @@
|
|
|
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
|
{
|
|
18
|
+
[DataMember(Name = "State")]
|
|
19
|
+
[JsonPropertyName("State")]
|
|
14
20
|
public static IRandom Instance => ThreadLocalRandom<PcgRandom>.Instance;
|
|
15
21
|
|
|
16
|
-
[JsonInclude]
|
|
17
|
-
[JsonPropertyName("Increment")]
|
|
18
|
-
[DataMember(Name = "Increment")]
|
|
19
22
|
internal readonly ulong _increment;
|
|
20
23
|
|
|
21
|
-
[JsonInclude]
|
|
22
|
-
[JsonPropertyName("State")]
|
|
23
|
-
[DataMember(Name = "State")]
|
|
24
24
|
internal ulong _state;
|
|
25
25
|
|
|
26
|
-
public PcgRandom()
|
|
26
|
+
public PcgRandom()
|
|
27
|
+
: this(Guid.NewGuid()) { }
|
|
27
28
|
|
|
28
29
|
public PcgRandom(Guid guid)
|
|
29
30
|
{
|
|
@@ -32,14 +33,14 @@
|
|
|
32
33
|
_increment = BitConverter.ToUInt64(guidArray, sizeof(ulong));
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
[JsonConstructor]
|
|
37
|
+
public PcgRandom(RandomState state)
|
|
36
38
|
{
|
|
37
|
-
_state =
|
|
38
|
-
_increment =
|
|
39
|
-
_cachedGaussian =
|
|
39
|
+
_state = state.State1;
|
|
40
|
+
_increment = state.State2;
|
|
41
|
+
_cachedGaussian = state.Gaussian;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
[JsonConstructor]
|
|
43
44
|
public PcgRandom(ulong increment, ulong state)
|
|
44
45
|
{
|
|
45
46
|
_increment = increment;
|
|
@@ -76,7 +77,14 @@
|
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
// ReSharper disable once CompareOfFloatsByEqualityOperator
|
|
79
|
-
return _increment == other._increment
|
|
80
|
+
return _increment == other._increment
|
|
81
|
+
&& _state == other._state
|
|
82
|
+
&& _cachedGaussian == other._cachedGaussian;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public override bool Equals(object obj)
|
|
86
|
+
{
|
|
87
|
+
return Equals(obj as PcgRandom);
|
|
80
88
|
}
|
|
81
89
|
|
|
82
90
|
public int CompareTo(PcgRandom other)
|
|
@@ -120,9 +128,9 @@
|
|
|
120
128
|
return _cachedGaussian.Value.CompareTo(other._cachedGaussian.Value);
|
|
121
129
|
}
|
|
122
130
|
|
|
123
|
-
public
|
|
131
|
+
public int CompareTo(object obj)
|
|
124
132
|
{
|
|
125
|
-
return
|
|
133
|
+
return CompareTo(obj as PcgRandom);
|
|
126
134
|
}
|
|
127
135
|
|
|
128
136
|
public override int GetHashCode()
|
|
@@ -135,14 +143,9 @@
|
|
|
135
143
|
return $"{{\"Increment\": {_increment}, \"State\": {_state}}}";
|
|
136
144
|
}
|
|
137
145
|
|
|
138
|
-
public int CompareTo(object other)
|
|
139
|
-
{
|
|
140
|
-
return CompareTo(other as PcgRandom);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
146
|
public override IRandom Copy()
|
|
144
147
|
{
|
|
145
148
|
return new PcgRandom(InternalState);
|
|
146
149
|
}
|
|
147
150
|
}
|
|
148
|
-
}
|
|
151
|
+
}
|
|
@@ -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,6 +1,7 @@
|
|
|
1
1
|
namespace UnityHelpers.Core.Random
|
|
2
2
|
{
|
|
3
3
|
using System;
|
|
4
|
+
using System.Text.Json.Serialization;
|
|
4
5
|
using Extension;
|
|
5
6
|
using Helper;
|
|
6
7
|
using ProtoBuf;
|
|
@@ -27,6 +28,7 @@
|
|
|
27
28
|
|
|
28
29
|
[ProtoMember(1)]
|
|
29
30
|
private ulong _state1;
|
|
31
|
+
|
|
30
32
|
[ProtoMember(2)]
|
|
31
33
|
private ulong _state2;
|
|
32
34
|
|
|
@@ -38,6 +40,7 @@
|
|
|
38
40
|
|
|
39
41
|
private int _hashCode;
|
|
40
42
|
|
|
43
|
+
[JsonConstructor]
|
|
41
44
|
public RandomState(ulong state1, ulong state2 = 0, double? gaussian = null)
|
|
42
45
|
{
|
|
43
46
|
_state1 = state1;
|
|
@@ -71,14 +74,22 @@
|
|
|
71
74
|
public bool Equals(RandomState other)
|
|
72
75
|
{
|
|
73
76
|
// ReSharper disable once CompareOfFloatsByEqualityOperator
|
|
74
|
-
return _state1 == other._state1
|
|
77
|
+
return _state1 == other._state1
|
|
78
|
+
&& _state2 == other._state2
|
|
79
|
+
&& _hasGaussian == other._hasGaussian
|
|
80
|
+
&& (!_hasGaussian || _gaussian == other._gaussian);
|
|
75
81
|
}
|
|
76
82
|
|
|
77
83
|
public override int GetHashCode()
|
|
78
84
|
{
|
|
79
85
|
if (_hashCode == 0)
|
|
80
86
|
{
|
|
81
|
-
return _hashCode = Objects.ValueTypeHashCode(
|
|
87
|
+
return _hashCode = Objects.ValueTypeHashCode(
|
|
88
|
+
_state1,
|
|
89
|
+
_state2,
|
|
90
|
+
_hasGaussian,
|
|
91
|
+
_gaussian
|
|
92
|
+
);
|
|
82
93
|
}
|
|
83
94
|
|
|
84
95
|
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,29 @@
|
|
|
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
|
+
[JsonPropertyName("State")]
|
|
20
|
+
[DataMember(Name = "State")]
|
|
21
|
+
public override RandomState InternalState => new(_position, gaussian: _cachedGaussian);
|
|
14
22
|
|
|
15
23
|
private uint _position;
|
|
16
24
|
|
|
17
|
-
public SquirrelRandom()
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
25
|
+
public SquirrelRandom()
|
|
26
|
+
: this(Guid.NewGuid().GetHashCode()) { }
|
|
21
27
|
|
|
22
28
|
public SquirrelRandom(int seed)
|
|
23
29
|
{
|
|
24
30
|
_position = unchecked((uint)seed);
|
|
25
31
|
}
|
|
26
32
|
|
|
33
|
+
[JsonConstructor]
|
|
27
34
|
public SquirrelRandom(RandomState randomState)
|
|
28
35
|
{
|
|
29
36
|
_position = unchecked((uint)randomState.State1);
|
|
30
37
|
_cachedGaussian = randomState.Gaussian;
|
|
31
38
|
}
|
|
32
39
|
|
|
33
|
-
public override RandomState InternalState => new(_position, gaussian: _cachedGaussian);
|
|
34
|
-
|
|
35
40
|
public override uint NextUint()
|
|
36
41
|
{
|
|
37
42
|
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,12 +1,21 @@
|
|
|
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
|
/// <summary>
|
|
6
8
|
/// Implementation dependent upon .Net's Random class.
|
|
7
9
|
/// </summary>
|
|
10
|
+
[Serializable]
|
|
11
|
+
[DataContract]
|
|
8
12
|
public sealed class SystemRandom : AbstractRandom
|
|
9
13
|
{
|
|
14
|
+
[JsonPropertyName("State")]
|
|
15
|
+
[DataMember(Name = "State")]
|
|
16
|
+
public override RandomState InternalState =>
|
|
17
|
+
new(unchecked((ulong)inext), unchecked((ulong)inextp), _cachedGaussian);
|
|
18
|
+
|
|
10
19
|
/*
|
|
11
20
|
Copied from Random.cs source. Apparently it isn't guaranteed to be the
|
|
12
21
|
same across platforms and we depend on that.
|
|
@@ -17,9 +26,8 @@
|
|
|
17
26
|
|
|
18
27
|
public static IRandom Instance => ThreadLocalRandom<SystemRandom>.Instance;
|
|
19
28
|
|
|
20
|
-
public SystemRandom()
|
|
21
|
-
|
|
22
|
-
}
|
|
29
|
+
public SystemRandom()
|
|
30
|
+
: this(Environment.TickCount) { }
|
|
23
31
|
|
|
24
32
|
public SystemRandom(int seed)
|
|
25
33
|
{
|
|
@@ -48,6 +56,7 @@
|
|
|
48
56
|
this.inextp = 21;
|
|
49
57
|
}
|
|
50
58
|
|
|
59
|
+
[JsonConstructor]
|
|
51
60
|
public SystemRandom(RandomState randomState)
|
|
52
61
|
{
|
|
53
62
|
inext = unchecked((int)randomState.State1);
|
|
@@ -55,8 +64,6 @@
|
|
|
55
64
|
_cachedGaussian = randomState.Gaussian;
|
|
56
65
|
}
|
|
57
66
|
|
|
58
|
-
public override RandomState InternalState => new(unchecked((ulong)inext), unchecked((ulong)inextp), _cachedGaussian);
|
|
59
|
-
|
|
60
67
|
public override uint NextUint()
|
|
61
68
|
{
|
|
62
69
|
int inext = this.inext;
|
|
@@ -75,7 +82,7 @@
|
|
|
75
82
|
this.SeedArray[index1] = num;
|
|
76
83
|
this.inext = index1;
|
|
77
84
|
this.inextp = index2;
|
|
78
|
-
return unchecked((uint)
|
|
85
|
+
return unchecked((uint)num);
|
|
79
86
|
}
|
|
80
87
|
|
|
81
88
|
public override double NextDouble()
|
|
@@ -84,15 +91,14 @@
|
|
|
84
91
|
do
|
|
85
92
|
{
|
|
86
93
|
generated = unchecked((int)NextUint()) * 4.6566128752458E-10;
|
|
87
|
-
}
|
|
88
|
-
while (generated < 0 || 1 <= generated);
|
|
94
|
+
} while (generated < 0 || 1 <= generated);
|
|
89
95
|
|
|
90
96
|
return generated;
|
|
91
97
|
}
|
|
92
98
|
|
|
93
99
|
public override float NextFloat()
|
|
94
100
|
{
|
|
95
|
-
return (float)
|
|
101
|
+
return (float)NextDouble();
|
|
96
102
|
}
|
|
97
103
|
|
|
98
104
|
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,51 @@
|
|
|
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
|
+
[JsonPropertyName("State")]
|
|
14
|
+
[DataMember(Name = "State")]
|
|
15
|
+
public override RandomState InternalState
|
|
10
16
|
{
|
|
17
|
+
get
|
|
18
|
+
{
|
|
19
|
+
unchecked
|
|
20
|
+
{
|
|
21
|
+
return new RandomState(
|
|
22
|
+
(ulong)(_seed ?? 0),
|
|
23
|
+
gaussian: _seed != null ? 0.0 : null
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private readonly int? _seed;
|
|
30
|
+
|
|
31
|
+
public UnityRandom(int? seed = null)
|
|
32
|
+
{
|
|
33
|
+
if (seed != null)
|
|
34
|
+
{
|
|
35
|
+
_seed = seed.Value;
|
|
36
|
+
UnityEngine.Random.InitState(seed.Value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
[JsonConstructor]
|
|
41
|
+
public UnityRandom(RandomState state)
|
|
42
|
+
{
|
|
43
|
+
unchecked
|
|
44
|
+
{
|
|
45
|
+
_seed = state.Gaussian != null ? (int)state.State1 : null;
|
|
46
|
+
}
|
|
11
47
|
}
|
|
12
48
|
|
|
13
|
-
public override RandomState InternalState => throw new NotSupportedException("Unity Random does not expose its internal state");
|
|
14
49
|
public override uint NextUint()
|
|
15
50
|
{
|
|
16
51
|
return unchecked((uint)UnityEngine.Random.Range(int.MinValue, int.MaxValue));
|
|
@@ -18,7 +53,7 @@
|
|
|
18
53
|
|
|
19
54
|
public override IRandom Copy()
|
|
20
55
|
{
|
|
21
|
-
|
|
56
|
+
return new UnityRandom(_seed);
|
|
22
57
|
}
|
|
23
58
|
}
|
|
24
59
|
}
|
|
@@ -0,0 +1,123 @@
|
|
|
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
|
+
[JsonPropertyName("State")]
|
|
20
|
+
[DataMember(Name = "State")]
|
|
21
|
+
public override RandomState InternalState => new RandomState(_state);
|
|
22
|
+
|
|
23
|
+
private ulong _state;
|
|
24
|
+
|
|
25
|
+
public WyRandom()
|
|
26
|
+
: this(Guid.NewGuid()) { }
|
|
27
|
+
|
|
28
|
+
public WyRandom(Guid guid)
|
|
29
|
+
{
|
|
30
|
+
byte[] guidArray = guid.ToByteArray();
|
|
31
|
+
_state = BitConverter.ToUInt64(guidArray, 0);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
[JsonConstructor]
|
|
35
|
+
public WyRandom(RandomState randomState)
|
|
36
|
+
{
|
|
37
|
+
_state = randomState.State1;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public WyRandom(ulong state)
|
|
41
|
+
{
|
|
42
|
+
_state = state;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public override uint NextUint()
|
|
46
|
+
{
|
|
47
|
+
unchecked
|
|
48
|
+
{
|
|
49
|
+
_state += Prime0;
|
|
50
|
+
return (uint)Mum(_state ^ Prime1, _state);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/// <summary>
|
|
55
|
+
/// Perform a MUM (MUltiply and Mix) operation. Multiplies 2 unsigned 64-bit integers, then combines the
|
|
56
|
+
/// hi and lo bits of the resulting 128-bit integer using XOR
|
|
57
|
+
/// </summary>
|
|
58
|
+
/// <param name="x">First 64-bit integer</param>
|
|
59
|
+
/// <param name="y">Second 64-bit integer</param>
|
|
60
|
+
/// <returns>Result of the MUM (MUltiply and Mix) operation</returns>
|
|
61
|
+
private static ulong Mum(ulong x, ulong y)
|
|
62
|
+
{
|
|
63
|
+
(ulong hi, ulong lo) = Multiply64(x, y);
|
|
64
|
+
return hi ^ lo;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// <summary>
|
|
68
|
+
/// Multiplies 2 unsigned 64-bit integers, returning the result in 2 ulongs representing the hi and lo bits
|
|
69
|
+
/// of the resulting 128-bit integer
|
|
70
|
+
///
|
|
71
|
+
/// Source: https://stackoverflow.com/a/51587262/25758, but with a faster lo calculation
|
|
72
|
+
/// </summary>
|
|
73
|
+
/// <remarks>
|
|
74
|
+
/// <seealso cref="System.Numerics.BigInteger"/> can perform multiplication on large integers, but it's
|
|
75
|
+
/// comparatively slow, and an equivalent method allocates around 360B/call
|
|
76
|
+
/// </remarks>
|
|
77
|
+
/// <param name="x">First 64-bit integer</param>
|
|
78
|
+
/// <param name="y">Second 64-bit integer</param>
|
|
79
|
+
/// <returns>Product of <paramref name="x"/> and <paramref name="y"/></returns>
|
|
80
|
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
81
|
+
[SuppressMessage("ReSharper", "JoinDeclarationAndInitializer")]
|
|
82
|
+
private static unsafe (ulong Hi, ulong Lo) Multiply64(ulong x, ulong y)
|
|
83
|
+
{
|
|
84
|
+
ulong hi;
|
|
85
|
+
ulong lo;
|
|
86
|
+
|
|
87
|
+
// Use BMI2 intrinsics where available
|
|
88
|
+
#if NETCOREAPP3_0_OR_GREATER
|
|
89
|
+
if (System.Runtime.Intrinsics.X86.Bmi2.X64.IsSupported)
|
|
90
|
+
{
|
|
91
|
+
hi = System.Runtime.Intrinsics.X86.Bmi2.X64.MultiplyNoFlags(x, y, &lo);
|
|
92
|
+
return (hi, lo);
|
|
93
|
+
}
|
|
94
|
+
#endif
|
|
95
|
+
|
|
96
|
+
lo = x * y;
|
|
97
|
+
|
|
98
|
+
ulong x0 = (uint)x;
|
|
99
|
+
ulong x1 = x >> 32;
|
|
100
|
+
|
|
101
|
+
ulong y0 = (uint)y;
|
|
102
|
+
ulong y1 = y >> 32;
|
|
103
|
+
|
|
104
|
+
ulong p11 = x1 * y1;
|
|
105
|
+
ulong p01 = x0 * y1;
|
|
106
|
+
ulong p10 = x1 * y0;
|
|
107
|
+
ulong p00 = x0 * y0;
|
|
108
|
+
|
|
109
|
+
// 64-bit product + two 32-bit values
|
|
110
|
+
ulong middle = p10 + (p00 >> 32) + (uint)p01;
|
|
111
|
+
|
|
112
|
+
// 64-bit product + two 32-bit values
|
|
113
|
+
hi = p11 + (middle >> 32) + (p01 >> 32);
|
|
114
|
+
|
|
115
|
+
return (hi, lo);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public override IRandom Copy()
|
|
119
|
+
{
|
|
120
|
+
return new WyRandom(_state);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -2,25 +2,29 @@
|
|
|
2
2
|
{
|
|
3
3
|
using System;
|
|
4
4
|
using System.Runtime.Serialization;
|
|
5
|
+
using System.Text.Json.Serialization;
|
|
5
6
|
|
|
6
7
|
[Serializable]
|
|
8
|
+
[DataContract]
|
|
7
9
|
public sealed class XorShiftRandom : AbstractRandom
|
|
8
10
|
{
|
|
9
11
|
public static IRandom Instance => ThreadLocalRandom<XorShiftRandom>.Instance;
|
|
10
|
-
public override RandomState InternalState => new(_state, 0, _cachedGaussian);
|
|
11
12
|
|
|
13
|
+
[JsonPropertyName("State")]
|
|
12
14
|
[DataMember(Name = "State")]
|
|
15
|
+
public override RandomState InternalState => new(_state, 0, _cachedGaussian);
|
|
16
|
+
|
|
13
17
|
private uint _state;
|
|
14
18
|
|
|
15
|
-
public XorShiftRandom()
|
|
16
|
-
|
|
17
|
-
}
|
|
19
|
+
public XorShiftRandom()
|
|
20
|
+
: this(Guid.NewGuid().GetHashCode()) { }
|
|
18
21
|
|
|
19
22
|
public XorShiftRandom(int state)
|
|
20
23
|
{
|
|
21
24
|
_state = unchecked((uint)state);
|
|
22
25
|
}
|
|
23
26
|
|
|
27
|
+
[JsonConstructor]
|
|
24
28
|
public XorShiftRandom(RandomState state)
|
|
25
29
|
{
|
|
26
30
|
_state = unchecked((uint)state.State1);
|
package/Runtime/Core/Random.meta
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
fileFormatVersion: 2
|
|
2
|
-
guid: 89615d17d6d576a4a9c877765fb8abdb
|
|
3
|
-
folderAsset: yes
|
|
4
|
-
DefaultImporter:
|
|
5
|
-
externalObjects: {}
|
|
6
|
-
userData:
|
|
7
|
-
assetBundleName:
|
|
8
|
-
assetBundleVariant:
|
|
1
|
+
fileFormatVersion: 2
|
|
2
|
+
guid: 89615d17d6d576a4a9c877765fb8abdb
|
|
3
|
+
folderAsset: yes
|
|
4
|
+
DefaultImporter:
|
|
5
|
+
externalObjects: {}
|
|
6
|
+
userData:
|
|
7
|
+
assetBundleName:
|
|
8
|
+
assetBundleVariant:
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
fileFormatVersion: 2
|
|
2
|
-
guid: 1e671babce85bff4a96cf95c1ed6e45a
|
|
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: 1e671babce85bff4a96cf95c1ed6e45a
|
|
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: c54b5af6a3d3cb5488395c0aab58d0f1
|
|
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: c54b5af6a3d3cb5488395c0aab58d0f1
|
|
3
|
+
MonoImporter:
|
|
4
|
+
externalObjects: {}
|
|
5
|
+
serializedVersion: 2
|
|
6
|
+
defaultReferences: []
|
|
7
|
+
executionOrder: 0
|
|
8
|
+
icon: {instanceID: 0}
|
|
9
|
+
userData:
|
|
10
|
+
assetBundleName:
|
|
11
|
+
assetBundleVariant:
|