Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SignsOfLife.Utils
- {
- public class ThreadSafeRandom
- {
- private static readonly Random _global = new Random();
- [ThreadStatic] private static Random _local;
- public int Next()
- {
- if (_local == null)
- {
- lock (_global)
- {
- if (_local == null)
- {
- int seed = _global.Next();
- _local = new Random(seed);
- }
- }
- }
- return _local.Next();
- }
- public int Next(int minValue, int maxValue)
- {
- if (_local == null)
- {
- lock (_global)
- {
- if (_local == null)
- {
- int seed = _global.Next();
- _local = new Random(seed);
- }
- }
- }
- return _local.Next(minValue, maxValue);
- }
- public int Next(int maxValue)
- {
- if (_local == null)
- {
- lock (_global)
- {
- if (_local == null)
- {
- int seed = _global.Next();
- _local = new Random(seed);
- }
- }
- }
- return _local.Next(maxValue);
- }
- public double NextDouble()
- {
- if (_local == null)
- {
- lock (_global)
- {
- if (_local == null)
- {
- int seed = _global.Next();
- _local = new Random(seed);
- }
- }
- }
- return _local.NextDouble();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement