// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using UnityEngine; namespace Microsoft.MixedReality.GraphicsTools { /// /// A implementation of Unity's Random class which is safe to call from multiple threads. /// public static class ThreadSafeRandom { [System.ThreadStatic] private static System.Random local; private static System.Random global = new System.Random(); /// /// Returns a random float within [0.0..1.0] (range is inclusive) (Read Only). /// public static float value { get { Initialize(); return (float)(local.NextDouble()); } } /// /// Returns a random int within [minInclusive..maxInclusive] (range is inclusive). /// public static int Range(int min, int max) { Initialize(); return local.Next(min, max); } /// /// Returns a random float within [minInclusive..maxInclusive] (range is inclusive). /// public static float Range(float min, float max) { return value * (max - min) + min; } /// /// Returns a random point inside or on a circle with radius 1.0 (Read Only). /// public static Vector2 insideUnitCircle { get { return new Vector2(value * 2.0f - 1.0f, value * 2.0f - 1.0f).normalized; } } /// /// Returns a random point on the surface of a sphere with radius 1.0 (Read Only). /// public static Vector3 onUnitSphere { get { return new Vector3(value * 2.0f - 1.0f, value * 2.0f - 1.0f, value * 2.0f - 1.0f).normalized; } } /// /// Returns a random point inside or on a sphere with radius 1.0 (Read Only). /// public static Vector3 insideUnitSphere { get { return onUnitSphere * value; } } /// /// Returns a random rotation (Read Only). /// public static Quaternion rotation { get { return new Quaternion(value, value, value, value); } } private static void Initialize() { if (local == null) { int seed; lock (global) { seed = global.Next(); } local = new System.Random(seed); } } } }