Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 4ab89bb

Browse files
committed
Add shared random implementation
1 parent e216f55 commit 4ab89bb

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>2.0.0-alpha3</Version>
3+
<Version>2.0.0-alpha4</Version>
44
</PropertyGroup>
55

66
<PropertyGroup>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// _ __ __
2+
// ___ ___ ___ _ __ __| | __ _ | \/ |
3+
// / __|/ __| / _ \| '_ \ / _` | / _` || |\/| |
4+
// \__ \\__ \| __/| | | || (_| || (_| || | | |
5+
// |___/|___/ \___||_| |_| \__,_| \__,_||_| |_|
6+
// |
7+
// Copyright 2021-2021 Łukasz "JustArchi" Domeradzki
8+
// Contact: [email protected]
9+
// |
10+
// Licensed under the Apache License, Version 2.0 (the "License");
11+
// you may not use this file except in compliance with the License.
12+
// You may obtain a copy of the License at
13+
// |
14+
// http://www.apache.org/licenses/LICENSE-2.0
15+
// |
16+
// Unless required by applicable law or agreed to in writing, software
17+
// distributed under the License is distributed on an "AS IS" BASIS,
18+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
// See the License for the specific language governing permissions and
20+
// limitations under the License.
21+
22+
using System;
23+
24+
namespace JustArchiNET.Madness.Internal;
25+
26+
internal sealed class ThreadSafeRandom : Random {
27+
private readonly object LockObject = new();
28+
29+
#pragma warning disable CA5394 // The caller decides to call this, not us
30+
public override int Next() {
31+
lock (LockObject) {
32+
return base.Next();
33+
}
34+
}
35+
36+
public override int Next(int maxValue) {
37+
lock (LockObject) {
38+
return base.Next(maxValue);
39+
}
40+
}
41+
42+
public override int Next(int minValue, int maxValue) {
43+
lock (LockObject) {
44+
return base.Next(minValue, maxValue);
45+
}
46+
}
47+
48+
public override void NextBytes(byte[] buffer) {
49+
lock (LockObject) {
50+
base.NextBytes(buffer);
51+
}
52+
}
53+
54+
public override double NextDouble() {
55+
lock (LockObject) {
56+
return base.NextDouble();
57+
}
58+
}
59+
60+
protected override double Sample() {
61+
lock (LockObject) {
62+
return base.Sample();
63+
}
64+
}
65+
#pragma warning restore CA5394 // The caller decides to call this, not us
66+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// _ __ __
2+
// ___ ___ ___ _ __ __| | __ _ | \/ |
3+
// / __|/ __| / _ \| '_ \ / _` | / _` || |\/| |
4+
// \__ \\__ \| __/| | | || (_| || (_| || | | |
5+
// |___/|___/ \___||_| |_| \__,_| \__,_||_| |_|
6+
// |
7+
// Copyright 2021-2021 Łukasz "JustArchi" Domeradzki
8+
// Contact: [email protected]
9+
// |
10+
// Licensed under the Apache License, Version 2.0 (the "License");
11+
// you may not use this file except in compliance with the License.
12+
// You may obtain a copy of the License at
13+
// |
14+
// http://www.apache.org/licenses/LICENSE-2.0
15+
// |
16+
// Unless required by applicable law or agreed to in writing, software
17+
// distributed under the License is distributed on an "AS IS" BASIS,
18+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
// See the License for the specific language governing permissions and
20+
// limitations under the License.
21+
22+
using JetBrains.Annotations;
23+
using JustArchiNET.Madness.Helpers;
24+
using JustArchiNET.Madness.Internal;
25+
26+
namespace JustArchiNET.Madness.RandomMadness;
27+
28+
[MadnessType(EMadnessType.Replacement)]
29+
[PublicAPI]
30+
public static class Random {
31+
public static System.Random Shared { get; } = new ThreadSafeRandom();
32+
}

0 commit comments

Comments
 (0)