diff --git a/Assets/Code/Frontend/RandomTest.cs b/Assets/Code/Frontend/RandomTest.cs index 100634c..a49da3a 100644 --- a/Assets/Code/Frontend/RandomTest.cs +++ b/Assets/Code/Frontend/RandomTest.cs @@ -1,261 +1,291 @@ -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using Testing; -using UnityEngine; - -namespace Frontend -{ - public class TestFront - { - public Texture2D CoordTexture; - public bool Enabled; - public bool Initialized; - public Texture2D NoiseTexture; - public RandomnessTest Test; - } - - public class RandomTest : MonoBehaviour - { - private const int Width = 256; - - private readonly List tests = new List(); - - [SerializeField] - private Font _font; - - [SerializeField] - private RandomSequence[] _sequences; - - private GUIStyle button; - - private GUIStyle header; - private bool initializeAllStarted; - private int lastScreenHeight; - private GUIStyle loading; - - private Vector2 optionsScroll; - - private int progress; - private Vector2 scroll; - private bool showCoords = true; - - private bool showNoise = true; - private bool showOptions; - private bool showStats = true; - private GUIStyle stats; - private GUIStyle style; - - private void Start() - { - foreach (var sequence in _sequences) - tests.Add(new TestFront - { - Test = new RandomnessTest(sequence) - }); - } - - public void InitializeAll() - { - if (initializeAllStarted) - return; - initializeAllStarted = true; - StartCoroutine(Initialization()); - } - - private IEnumerator Initialization() - { - foreach (var test in tests) - { - if (test.Initialized) - continue; - - yield return null; - Initialize(test); - } - } - - private void Initialize(TestFront front) - { - if (front.Initialized) - return; - front.Initialized = true; - - var test = front.Test; - test.Test(); - - front.NoiseTexture = new Texture2D(256, 256) {filterMode = FilterMode.Point}; - test.Reset(); - for (var j = front.NoiseTexture.height - 1; j >= 0; j--) - for (var i = 0; i < front.NoiseTexture.width; i++) - { - var f = test.NoiseSequence[i + j * 256]; - front.NoiseTexture.SetPixel(i, j, new Color(f, f, f, 1)); - } - - front.NoiseTexture.Apply(); - - front.CoordTexture = new Texture2D(256, 256) {filterMode = FilterMode.Point}; - for (var j = front.CoordTexture.height - 1; j >= 0; j--) - for (var i = 0; i < front.CoordTexture.width; i++) - { - var f = 1.0f - test.CoordsArray[i, j]; - front.CoordTexture.SetPixel(i, j, new Color(f, f, f, 1)); - } - - front.CoordTexture.Apply(); - - progress++; - } - - private void OnGUI() - { - if (Event.current.type == EventType.Repaint) - GL.Clear(false, true, Color.white); - var screenHeight = Screen.height; - var screenWidth = Screen.width; - var pt = screenHeight / 100f; - - var menuHeight = screenHeight * 0.1f; - var bottomLine = screenHeight - menuHeight; - - if (style == null || lastScreenHeight != screenHeight) - { - style = new GUIStyle("label") - { - font = _font, - normal = {textColor = Color.black}, - fontSize = Mathf.Max(10, (int) (screenHeight * 0.01f)), - alignment = TextAnchor.UpperCenter - }; - stats = new GUIStyle("label") - { - font = _font, - normal = {textColor = Color.black}, - fontSize = Mathf.Max(10, (int) (screenHeight * 0.01f)) - }; - header = new GUIStyle("label") - { - normal = {textColor = Color.black}, - fontSize = Mathf.Max(12, (int) (screenHeight * 0.012f)), - fontStyle = FontStyle.Bold, - alignment = TextAnchor.UpperCenter - }; - loading = new GUIStyle("label") - { - normal = {textColor = Color.black}, - fontSize = Mathf.Max(12, (int) (screenHeight * 0.012f)), - fontStyle = FontStyle.Bold, - alignment = TextAnchor.UpperCenter - }; - button = new GUIStyle("button") - { - fontSize = Mathf.Max(12, (int) (screenHeight * 0.012f)), - fontStyle = FontStyle.Bold - }; - var scrollSize = screenHeight * 0.04f; - GUI.skin.horizontalScrollbar.fixedHeight = scrollSize; - GUI.skin.horizontalScrollbarThumb.fixedHeight = scrollSize; - GUI.skin.verticalScrollbar.fixedWidth = scrollSize; - GUI.skin.verticalScrollbarThumb.fixedWidth = scrollSize; - } - - lastScreenHeight = screenHeight; - - var active = tests.Count(t => t != null && t.Enabled && t.Initialized); - - var pageWidth = pt * 30; - scroll = GUI.BeginScrollView(new Rect(0, 0, screenWidth, bottomLine), scroll, - new Rect(0, 0, pageWidth * active, pageWidth * 3)); - - var count = tests.Count; - var offset = 0; - for (var i = 0; i < count; i++) - { - var front = tests[i]; - var test = front.Test; - - if (!front.Enabled || !front.Initialized) - continue; - - GUILayout.BeginArea(new Rect(offset * pageWidth, 0, pageWidth, pageWidth * 3)); - offset++; - - GUILayout.Label(test.Name, header); - - var textureSize = Mathf.Min(Width, pt * 30); - if (showNoise) - { - GUILayout.BeginHorizontal(); - GUILayout.FlexibleSpace(); - GUILayout.Label(front.NoiseTexture, GUILayout.Width(textureSize), GUILayout.Height(textureSize)); - GUILayout.FlexibleSpace(); - GUILayout.EndHorizontal(); - GUILayout.Label("Sequence of 65536 random values.", style); - } - - if (showCoords) - { - GUILayout.BeginHorizontal(); - GUILayout.FlexibleSpace(); - GUILayout.Label(front.CoordTexture, GUILayout.Width(textureSize), GUILayout.Height(textureSize)); - GUILayout.FlexibleSpace(); - GUILayout.EndHorizontal(); - GUILayout.Label("Plot of 500000 random coordinates.", style); - } - - if (showStats) - { - GUILayout.BeginHorizontal(); - GUILayout.FlexibleSpace(); - GUILayout.Label(test.Result, stats); - GUILayout.FlexibleSpace(); - GUILayout.EndHorizontal(); - } - - GUILayout.EndArea(); - } - - GUI.EndScrollView(); - - GUILayout.BeginArea(new Rect(0, bottomLine, screenWidth, menuHeight)); - var height = GUILayout.Height(menuHeight); - if (GUILayout.Button("Options", button, height)) - showOptions = !showOptions; - - GUILayout.EndArea(); - - if (showOptions) - GUILayout.Window(0, new Rect(0, 0, screenWidth, bottomLine), OptionsWindow, "Options", header); - - if (!initializeAllStarted) - { - if (GUI.Button(new Rect(0, bottomLine - menuHeight, screenWidth, menuHeight), - "Load All", button)) - InitializeAll(); - } - else if (progress != count) - { - GUI.Label(new Rect(0, bottomLine - menuHeight, screenWidth, menuHeight), - $"Loading {progress}/{count}", loading); - } - } - - private void OptionsWindow(int id) - { - optionsScroll = GUILayout.BeginScrollView(optionsScroll); - showNoise = GUILayout.Toggle(showNoise, "Show Noise Image", button); - showCoords = GUILayout.Toggle(showCoords, "Show Coordinates Plot", button); - showStats = GUILayout.Toggle(showStats, "Show Statistics", button); - - foreach (var front in tests) - { - front.Enabled = GUILayout.Toggle(front.Enabled, front.Test.Name, button); - if (front.Enabled && !front.Initialized) - Initialize(front); - } - - GUILayout.EndScrollView(); - } - } -} +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using Testing; +using UnityEngine; + +namespace Frontend +{ + public class TestFront + { + public Texture2D CoordTexture; + public bool Enabled; + public bool Initialized; + public Texture2D NoiseTexture; + public RandomnessTest Test; + } + + public class RandomTest : MonoBehaviour + { + private const int Width = 256; + + private readonly List tests = new List(); + + [SerializeField] + private Font _font; + + [SerializeField] + private RandomSequence[] _sequences; + + private GUIStyle button; + + private GUIStyle header; + private bool initializeAllStarted; + private int lastScreenHeight; + private GUIStyle loading; + + private Vector2 optionsScroll; + + private int progress; + private Vector2 scroll; + private bool showCoords = true; + + private bool showNoise = true; + private bool showOptions; + private bool showStats = true; + private GUIStyle stats; + private GUIStyle style; + + private static void SaveTextureAsPNG(Texture2D texture, string fullPath) + { + byte[] bytes = texture.EncodeToPNG(); + System.IO.File.WriteAllBytes(fullPath, bytes); + } + + private void Start() + { + foreach (var sequence in _sequences) + tests.Add(new TestFront + { + Test = new RandomnessTest(sequence) + }); + } + + public void InitializeAll() + { + if (initializeAllStarted) + return; + initializeAllStarted = true; + StartCoroutine(Initialization()); + } + + private void SaveRandomSequence(TestFront front, string path) + { + var dirName = System.IO.Path.GetDirectoryName(path); + if (!System.IO.Directory.Exists(dirName)) + { + if (string.IsNullOrEmpty(dirName)) + { + Debug.LogError("Path is null"); + } + else + { + System.IO.Directory.CreateDirectory(dirName); + } + } + + SaveTextureAsPNG(front.CoordTexture, path + front.Test.Name + " coordinate.png"); + SaveTextureAsPNG(front.NoiseTexture, path + front.Test.Name + " noise.png"); + Debug.Log(Application.dataPath + front.Test.Name + " saved."); + } + + private IEnumerator Initialization() + { + foreach (var test in tests) + { + if (test.Initialized) + continue; + + yield return null; + Initialize(test); + if (test.Initialized) + { + SaveRandomSequence(test,Application.dataPath + "/results_images/"); + } + } + } + + private void Initialize(TestFront front) + { + if (front.Initialized) + return; + front.Initialized = true; + + var test = front.Test; + test.Test(); + + front.NoiseTexture = new Texture2D(256, 256) {filterMode = FilterMode.Point}; + test.Reset(); + for (var j = front.NoiseTexture.height - 1; j >= 0; j--) + for (var i = 0; i < front.NoiseTexture.width; i++) + { + var color = RandomnessTest.GetColor(test.NoiseSequence[i + j * 256]); + front.NoiseTexture.SetPixel(i, j, color); + } + + front.NoiseTexture.Apply(); + + front.CoordTexture = new Texture2D(256, 256) {filterMode = FilterMode.Point}; + for (var j = front.CoordTexture.height - 1; j >= 0; j--) + for (var i = 0; i < front.CoordTexture.width; i++) + { + var f = 1.0f - test.CoordsArray[i, j]; + front.CoordTexture.SetPixel(i, j, new Color(f, f, f, 1)); + } + + front.CoordTexture.Apply(); + + progress++; + } + + private void OnGUI() + { + if (Event.current.type == EventType.Repaint) + GL.Clear(false, true, Color.white); + var screenHeight = Screen.height; + var screenWidth = Screen.width; + var pt = screenHeight / 100f; + + var menuHeight = screenHeight * 0.1f; + var bottomLine = screenHeight - menuHeight; + + if (style == null || lastScreenHeight != screenHeight) + { + style = new GUIStyle("label") + { + font = _font, + normal = {textColor = Color.black}, + fontSize = Mathf.Max(10, (int) (screenHeight * 0.01f)), + alignment = TextAnchor.UpperCenter + }; + stats = new GUIStyle("label") + { + font = _font, + normal = {textColor = Color.black}, + fontSize = Mathf.Max(10, (int) (screenHeight * 0.01f)) + }; + header = new GUIStyle("label") + { + normal = {textColor = Color.black}, + fontSize = Mathf.Max(12, (int) (screenHeight * 0.012f)), + fontStyle = FontStyle.Bold, + alignment = TextAnchor.UpperCenter + }; + loading = new GUIStyle("label") + { + normal = {textColor = Color.black}, + fontSize = Mathf.Max(12, (int) (screenHeight * 0.012f)), + fontStyle = FontStyle.Bold, + alignment = TextAnchor.UpperCenter + }; + button = new GUIStyle("button") + { + fontSize = Mathf.Max(12, (int) (screenHeight * 0.012f)), + fontStyle = FontStyle.Bold + }; + var scrollSize = screenHeight * 0.04f; + GUI.skin.horizontalScrollbar.fixedHeight = scrollSize; + GUI.skin.horizontalScrollbarThumb.fixedHeight = scrollSize; + GUI.skin.verticalScrollbar.fixedWidth = scrollSize; + GUI.skin.verticalScrollbarThumb.fixedWidth = scrollSize; + } + + lastScreenHeight = screenHeight; + + var active = tests.Count(t => t != null && t.Enabled && t.Initialized); + + var pageWidth = pt * 30; + scroll = GUI.BeginScrollView(new Rect(0, 0, screenWidth, bottomLine), scroll, + new Rect(0, 0, pageWidth * active, pageWidth * 3)); + + var count = tests.Count; + var offset = 0; + for (var i = 0; i < count; i++) + { + var front = tests[i]; + var test = front.Test; + + if (!front.Enabled || !front.Initialized) + continue; + + GUILayout.BeginArea(new Rect(offset * pageWidth, 0, pageWidth, pageWidth * 3)); + offset++; + + GUILayout.Label(test.Name, header); + + var textureSize = Mathf.Min(Width, pt * 30); + if (showNoise) + { + GUILayout.BeginHorizontal(); + GUILayout.FlexibleSpace(); + GUILayout.Label(front.NoiseTexture, GUILayout.Width(textureSize), GUILayout.Height(textureSize)); + GUILayout.FlexibleSpace(); + GUILayout.EndHorizontal(); + GUILayout.Label("Sequence of 65536 random values.", style); + } + + if (showCoords) + { + GUILayout.BeginHorizontal(); + GUILayout.FlexibleSpace(); + GUILayout.Label(front.CoordTexture, GUILayout.Width(textureSize), GUILayout.Height(textureSize)); + GUILayout.FlexibleSpace(); + GUILayout.EndHorizontal(); + GUILayout.Label("Plot of 500000 random coordinates.", style); + } + + if (showStats) + { + GUILayout.BeginHorizontal(); + GUILayout.FlexibleSpace(); + GUILayout.Label(test.Result, stats); + GUILayout.FlexibleSpace(); + GUILayout.EndHorizontal(); + } + + GUILayout.EndArea(); + } + + GUI.EndScrollView(); + + GUILayout.BeginArea(new Rect(0, bottomLine, screenWidth, menuHeight)); + var height = GUILayout.Height(menuHeight); + if (GUILayout.Button("Options", button, height)) + showOptions = !showOptions; + + GUILayout.EndArea(); + + if (showOptions) + GUILayout.Window(0, new Rect(0, 0, screenWidth, bottomLine), OptionsWindow, "Options", header); + + if (!initializeAllStarted) + { + if (GUI.Button(new Rect(0, bottomLine - menuHeight, screenWidth, menuHeight), + "Load All", button)) + InitializeAll(); + } + else if (progress != count) + { + GUI.Label(new Rect(0, bottomLine - menuHeight, screenWidth, menuHeight), + $"Loading {progress}/{count}", loading); + } + } + + private void OptionsWindow(int id) + { + optionsScroll = GUILayout.BeginScrollView(optionsScroll); + showNoise = GUILayout.Toggle(showNoise, "Show Noise Image", button); + showCoords = GUILayout.Toggle(showCoords, "Show Coordinates Plot", button); + showStats = GUILayout.Toggle(showStats, "Show Statistics", button); + + foreach (var front in tests) + { + front.Enabled = GUILayout.Toggle(front.Enabled, front.Test.Name, button); + if (front.Enabled && !front.Initialized) + Initialize(front); + } + + GUILayout.EndScrollView(); + } + } +} diff --git a/Assets/Code/Testing/RandomNumberGeneratorSequence.cs b/Assets/Code/Testing/RandomNumberGeneratorSequence.cs index cc1d2cf..ab73bc4 100644 --- a/Assets/Code/Testing/RandomNumberGeneratorSequence.cs +++ b/Assets/Code/Testing/RandomNumberGeneratorSequence.cs @@ -1,167 +1,167 @@ -using UnityEngine; - -namespace Testing -{ - public abstract class RandomSequence : ScriptableObject, IRandomSequence - { - public abstract void Reset(); - - public abstract uint Next(); - - public abstract string Name { get; } - } - - public abstract class RandomNumberGeneratorSequence : RandomSequence - where T : IRandomNumberGenerator, new() - { - private static T _wrapper; - - protected IRandomNumberGenerator Wrapper - { - get - { - if (_wrapper == null) - _wrapper = new T(); - return _wrapper; - } - } - } - - public abstract class RandomNumberGeneratorSeedSequence : RandomNumberGeneratorSequence - where T : IRandomNumberGenerator, new() - { - [SerializeField] - private int _seed; - - private TS generator; - - - public override string Name => Wrapper.IsSupportsSeed - ? $"{Wrapper.Name}\nnumbers # of seed {_seed}" - : $"{Wrapper.Name}\nnumbers #"; - - public override void Reset() - { - generator = Wrapper.Create(_seed); - } - - public override uint Next() - { - return Wrapper.Next(generator); - } - } - - public abstract class RandomNumberGeneratorVarySeedSequence : RandomNumberGeneratorSequence - where T : IRandomNumberGenerator, new() - { - [SerializeField] - private int _index; - - private int seed; - - public override string Name => $"{Wrapper.Name}\n{_index.Ordinal()} number of seed #"; - - private void Awake() - { - Reset(); - } - - public override void Reset() - { - seed = 0; - } - - public override uint Next() - { - var generator = Wrapper.Create(seed); - seed++; - for (var i = 0; i < _index; i++) - Wrapper.Next(generator); - return Wrapper.Next(generator); - } - } - - public abstract class HashSequence : RandomSequence - where T : IHashGenerator, new() - { - private static T _wrapper; - - protected IHashGenerator Wrapper - { - get - { - if (_wrapper == null) - _wrapper = new T(); - return _wrapper; - } - } - } - - public abstract class HashSeedSequence : HashSequence - where T : IHashGenerator, new() - { - [SerializeField] - private int _seed; - - private TS generator; - - private int index; - private bool initialized; - - private TS Generator - { - get - { - if (initialized) - return generator; - initialized = true; - return generator = Wrapper.Create(_seed); - } - } - - - public override string Name => Wrapper.IsSupportsSeed - ? $"{Wrapper.Name}\nnumbers # of seed {_seed}" - : $"{Wrapper.Name}\nnumbers #"; - - public override void Reset() - { - index = 0; - } - - public override uint Next() - { - var value = Wrapper.Hash(Generator, index); - index++; - return value; - } - } - - public abstract class HashVarySeedSequence : HashSequence - where T : IHashGenerator, new() - { - [SerializeField] - private int _index; - - private int seed; - - public override string Name => $"{Wrapper.Name}\n{_index.Ordinal()} number of seed #"; - - private void Awake() - { - Reset(); - } - - public override void Reset() - { - seed = 0; - } - - public override uint Next() - { - var value = Wrapper.Hash(Wrapper.Create(seed), _index); - seed++; - return value; - } - } -} +using UnityEngine; + +namespace Testing +{ + public abstract class RandomSequence : ScriptableObject, IRandomSequence + { + public abstract void Reset(); + + public abstract uint Next(); + + public abstract string Name { get; } + } + + public abstract class RandomNumberGeneratorSequence : RandomSequence + where T : IRandomNumberGenerator, new() + { + private static T _wrapper; + + protected IRandomNumberGenerator Wrapper + { + get + { + if (_wrapper == null) + _wrapper = new T(); + return _wrapper; + } + } + } + + public abstract class RandomNumberGeneratorSeedSequence : RandomNumberGeneratorSequence + where T : IRandomNumberGenerator, new() + { + [SerializeField] + private int _seed; + + private TS generator; + + + public override string Name => Wrapper.IsSupportsSeed + ? $"{Wrapper.Name}\nnumbers # of seed {_seed}" + : $"{Wrapper.Name}\nnumbers #"; + + public override void Reset() + { + generator = Wrapper.Create(_seed); + } + + public override uint Next() + { + return Wrapper.Next(generator); + } + } + + public abstract class RandomNumberGeneratorVarySeedSequence : RandomNumberGeneratorSequence + where T : IRandomNumberGenerator, new() + { + [SerializeField] + private int _index; + + private int seed; + + public override string Name => $"{Wrapper.Name}\n{_index.Ordinal()} number of seed #"; + + private void Awake() + { + Reset(); + } + + public override void Reset() + { + seed = 0; + } + + public override uint Next() + { + var generator = Wrapper.Create(seed); + seed++; + for (var i = 0; i < _index; i++) + Wrapper.Next(generator); + return Wrapper.Next(generator); + } + } + + public abstract class HashSequence : RandomSequence + where T : IHashGenerator, new() + { + private static T _wrapper; + + protected IHashGenerator Wrapper + { + get + { + if (_wrapper == null) + _wrapper = new T(); + return _wrapper; + } + } + } + + public abstract class HashSeedSequence : HashSequence + where T : IHashGenerator, new() + { + [SerializeField] + private int _seed; + + private TS generator; + + private int index; + private bool initialized; + + private TS Generator + { + get + { + if (generator != null && initialized) + return generator; + initialized = true; + return generator = Wrapper.Create(_seed); + } + } + + + public override string Name => Wrapper.IsSupportsSeed + ? $"{Wrapper.Name}\nnumbers # of seed {_seed}" + : $"{Wrapper.Name}\nnumbers #"; + + public override void Reset() + { + index = 0; + } + + public override uint Next() + { + var value = Wrapper.Hash(Generator, index); + index++; + return value; + } + } + + public abstract class HashVarySeedSequence : HashSequence + where T : IHashGenerator, new() + { + [SerializeField] + private int _index; + + private int seed; + + public override string Name => $"{Wrapper.Name}\n{_index.Ordinal()} number of seed #"; + + private void Awake() + { + Reset(); + } + + public override void Reset() + { + seed = 0; + } + + public override uint Next() + { + var value = Wrapper.Hash(Wrapper.Create(seed), _index); + seed++; + return value; + } + } +} diff --git a/Assets/Code/Testing/RandomnessTest.cs b/Assets/Code/Testing/RandomnessTest.cs index 1ccdce8..b7bb59b 100644 --- a/Assets/Code/Testing/RandomnessTest.cs +++ b/Assets/Code/Testing/RandomnessTest.cs @@ -1,145 +1,153 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * The Initial Developer of the Original Code is Rune Skovbo Johansen. - * Portions created by the Initial Developer are Copyright (C) 2015 - * the Initial Developer. All Rights Reserved. - */ - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using Ent; -using UnityEngine; - -namespace Testing -{ - public class RandomnessTest - { - private const int ByteIndex = 0; - private readonly int[,] coords = new int[256, 256]; - public readonly float[,] CoordsArray = new float[256, 256]; - private readonly float[] diagonalSums = new float[256]; - public readonly string Name; - public readonly float[] NoiseSequence = new float[256 * 256]; - private readonly IRandomSequence sequence; - private float diagonalsDeviation; - public string Result; - - public RandomnessTest(IRandomSequence randomSequence) - { - sequence = randomSequence; - Name = sequence.Name.Replace("#", "0\u2026n"); - } - - private uint GetBytePart(uint i, int byteIndex) - { - return ((i >> (8 * byteIndex)) % 256 + 256) % 256; - } - - public void Reset() - { - sequence.Reset(); - } - - public void Test() - { - var ints = new uint[500000]; - - // Call random function - sequence.Reset(); - var stopWatch = new Stopwatch(); - stopWatch.Start(); - for (var i = 0; i < ints.Length; i++) - ints[i] = sequence.Next(); - stopWatch.Stop(); - - // Convert to bytes - var bytes = new byte[ints.Length]; - for (var i = 0; i < bytes.Length; i++) - bytes[i] = (byte) GetBytePart(ints[i], ByteIndex); - - // Test randomness data - var ent = new EntCalc(false); - ent.AddSample(bytes); - var calcResult = ent.EndCalculation(); - - // Create noise sequence - for (var i = 0; i < NoiseSequence.Length; i++) - NoiseSequence[i] = GetBytePart(ints[i], ByteIndex) / 255f; - - // Create coords data - var max = 0; - for (var i = 0; i < ints.Length; i += 2) - { - var x = GetBytePart(ints[i], ByteIndex); - var y = GetBytePart(ints[i + 1], ByteIndex); - var value = coords[x, y]; - value++; - max = Mathf.Max(value, max); - coords[x, y] = value; - } - - // Calculate coords results - for (var j = 0; j < 256; j++) - for (var i = 0; i < 256; i++) - { - var value = coords[i, j] / (float) max; - CoordsArray[i, j] = value; - diagonalSums[(i + j) % 256] += value * 0.5f; - diagonalSums[(i - j + 256) % 256] += value * 0.5f; - } - - diagonalsDeviation = StandardDeviation(new List(diagonalSums)); - - // Get string with result - Result = GetResult(calcResult, stopWatch.ElapsedMilliseconds); - } - - private static float StandardDeviation(List valueList) - { - var m = 0.0f; - var s = 0.0f; - var k = 1; - foreach (var value in valueList) - { - var tmpM = m; - m += (value - tmpM) / k; - s += (value - tmpM) * (value - m); - k++; - } - - return (float) Math.Sqrt(s / (k - 1)); - } - - private string GetResult(EntCalc.EntCalcResult result, float duration) - { - var meanValueQuality = Clamp01(1 - Math.Abs(127.5 - result.Mean) / 128); - var serialCorrelationQuality = Clamp01(1 - 2 * Math.Abs(result.SerialCorrelation)); - var piQuality = Clamp01(1 - 10 * result.MonteCarloErrorPct); - var diagonalsDeviationQuality = Clamp01(1 - diagonalsDeviation / 256); - var combined = Math.Min(Math.Min(Math.Min(meanValueQuality, serialCorrelationQuality), piQuality), - diagonalsDeviationQuality); - - return " value quality\n" - + $"Entropy: {result.Entropy}\n" - + $"Mean Value: {result.Mean,8:F4} {meanValueQuality,7:P0}\n" - + $"Serial Correlation: {Math.Max(0, result.SerialCorrelation),8:F4} {serialCorrelationQuality,7:P0}\n" - + $"Monte Carlo Pi Value: {result.MonteCarloPiCalc,8:F4} {piQuality,7:P0}\n" - + $"Diagonals Deviation: {diagonalsDeviation,8:F4} {diagonalsDeviationQuality,7:P0}\n" - + $"Overall Quality: {combined,7:P0}\n\n" - + $"Execution Time: {duration,6} ms"; - } - - private static double Clamp01(double val) - { - return Clamp(val, 0, 1); - } - - private static double Clamp(double val, double min, double max) - { - return Math.Min(max, Math.Max(val, min)); - } - } -} +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * The Initial Developer of the Original Code is Rune Skovbo Johansen. + * Portions created by the Initial Developer are Copyright (C) 2015 + * the Initial Developer. All Rights Reserved. + */ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using Ent; +using UnityEngine; + +namespace Testing +{ + public class RandomnessTest + { + private const int ByteIndex = 0; + private readonly int[,] coords = new int[256, 256]; + public readonly float[,] CoordsArray = new float[256, 256]; + private readonly float[] diagonalSums = new float[256]; + public readonly string Name; + public readonly uint[] NoiseSequence = new uint[256 * 256]; + private readonly IRandomSequence sequence; + private float diagonalsDeviation; + public string Result; + + public RandomnessTest(IRandomSequence randomSequence) + { + sequence = randomSequence; + Name = sequence.Name.Replace("#", "0\u2026n"); + } + + public static Color GetColor(uint i) + { + float r = GetBytePart(i, 0) / 255f; + float g = GetBytePart(i, 1) / 255f; + float b = GetBytePart(i, 2) / 255f; + return new Color(r, g, b); + } + + public static uint GetBytePart(uint i, int byteIndex) + { + return ((i >> (8 * byteIndex)) % 256 + 256) % 256; + } + + public void Reset() + { + sequence.Reset(); + } + + public void Test() + { + var ints = new uint[500000]; + + // Call random function + sequence.Reset(); + var stopWatch = new Stopwatch(); + stopWatch.Start(); + for (var i = 0; i < ints.Length; i++) + ints[i] = sequence.Next(); + stopWatch.Stop(); + + // Convert to bytes + var bytes = new byte[ints.Length]; + for (var i = 0; i < bytes.Length; i++) + bytes[i] = (byte) GetBytePart(ints[i], ByteIndex); + + // Test randomness data + var ent = new EntCalc(false); + ent.AddSample(bytes); + var calcResult = ent.EndCalculation(); + + // Create noise sequence + for (var i = 0; i < NoiseSequence.Length; i++) + NoiseSequence[i] = ints[i]; + + // Create coords data + var max = 0; + for (var i = 0; i < ints.Length; i += 2) + { + var x = GetBytePart(ints[i], ByteIndex); + var y = GetBytePart(ints[i + 1], ByteIndex); + var value = coords[x, y]; + value++; + max = Mathf.Max(value, max); + coords[x, y] = value; + } + + // Calculate coords results + for (var j = 0; j < 256; j++) + for (var i = 0; i < 256; i++) + { + var value = coords[i, j] / (float) max; + CoordsArray[i, j] = value; + diagonalSums[(i + j) % 256] += value * 0.5f; + diagonalSums[(i - j + 256) % 256] += value * 0.5f; + } + + diagonalsDeviation = StandardDeviation(new List(diagonalSums)); + + // Get string with result + Result = GetResult(calcResult, stopWatch.ElapsedMilliseconds); + } + + private static float StandardDeviation(List valueList) + { + var m = 0.0f; + var s = 0.0f; + var k = 1; + foreach (var value in valueList) + { + var tmpM = m; + m += (value - tmpM) / k; + s += (value - tmpM) * (value - m); + k++; + } + + return (float) Math.Sqrt(s / (k - 1)); + } + + private string GetResult(EntCalc.EntCalcResult result, float duration) + { + var meanValueQuality = Clamp01(1 - Math.Abs(127.5 - result.Mean) / 128); + var serialCorrelationQuality = Clamp01(1 - 2 * Math.Abs(result.SerialCorrelation)); + var piQuality = Clamp01(1 - 10 * result.MonteCarloErrorPct); + var diagonalsDeviationQuality = Clamp01(1 - diagonalsDeviation / 256); + var combined = Math.Min(Math.Min(Math.Min(meanValueQuality, serialCorrelationQuality), piQuality), + diagonalsDeviationQuality); + + return " value quality\n" + + $"Entropy: {result.Entropy}\n" + + $"Mean Value: {result.Mean,8:F4} {meanValueQuality,7:P0}\n" + + $"Serial Correlation: {Math.Max(0, result.SerialCorrelation),8:F4} {serialCorrelationQuality,7:P0}\n" + + $"Monte Carlo Pi Value: {result.MonteCarloPiCalc,8:F4} {piQuality,7:P0}\n" + + $"Diagonals Deviation: {diagonalsDeviation,8:F4} {diagonalsDeviationQuality,7:P0}\n" + + $"Overall Quality: {combined,7:P0}\n\n" + + $"Execution Time: {duration,6} ms"; + } + + private static double Clamp01(double val) + { + return Clamp(val, 0, 1); + } + + private static double Clamp(double val, double min, double max) + { + return Math.Min(max, Math.Max(val, min)); + } + } +} diff --git a/Packages/manifest.json b/Packages/manifest.json index 5f4d7ff..5b6fcbd 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -1,8 +1,17 @@ { "dependencies": { - "com.unity.package-manager-ui": "2.0.8", - "com.unity.textmeshpro": "1.4.1", + "com.unity.2d.sprite": "1.0.0", + "com.unity.2d.tilemap": "1.0.0", + "com.unity.ide.rider": "1.1.4", + "com.unity.ide.vscode": "1.2.1", + "com.unity.multiplayer-hlapi": "1.0.6", + "com.unity.test-framework": "1.1.16", + "com.unity.textmeshpro": "2.0.1", + "com.unity.timeline": "1.2.6", + "com.unity.ugui": "1.0.0", + "com.unity.xr.legacyinputhelpers": "2.1.4", "com.unity.modules.ai": "1.0.0", + "com.unity.modules.androidjni": "1.0.0", "com.unity.modules.animation": "1.0.0", "com.unity.modules.assetbundle": "1.0.0", "com.unity.modules.audio": "1.0.0", diff --git a/ProjectSettings/EditorSettings.asset b/ProjectSettings/EditorSettings.asset index 5fcf984..bde857e 100644 --- a/ProjectSettings/EditorSettings.asset +++ b/ProjectSettings/EditorSettings.asset @@ -3,10 +3,33 @@ --- !u!159 &1 EditorSettings: m_ObjectHideFlags: 0 - serializedVersion: 3 + serializedVersion: 9 m_ExternalVersionControlSupport: Hidden Meta Files m_SerializationMode: 2 - m_WebSecurityEmulationEnabled: 0 - m_WebSecurityEmulationHostUrl: http://www.mydomain.com/mygame.unity3d + m_LineEndingsForNewScripts: 1 m_DefaultBehaviorMode: 0 + m_PrefabRegularEnvironment: {fileID: 0} + m_PrefabUIEnvironment: {fileID: 0} m_SpritePackerMode: 2 + m_SpritePackerPaddingPower: 1 + m_EtcTextureCompressorBehavior: 0 + m_EtcTextureFastCompressor: 2 + m_EtcTextureNormalCompressor: 2 + m_EtcTextureBestCompressor: 5 + m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd;asmdef;asmref;rsp;asmref + m_ProjectGenerationRootNamespace: + m_CollabEditorSettings: + inProgressEnabled: 1 + m_EnableTextureStreamingInEditMode: 1 + m_EnableTextureStreamingInPlayMode: 1 + m_AsyncShaderCompilation: 1 + m_EnterPlayModeOptionsEnabled: 0 + m_EnterPlayModeOptions: 3 + m_ShowLightmapResolutionOverlay: 1 + m_UseLegacyProbeSampleCount: 1 + m_AssetPipelineMode: 1 + m_CacheServerMode: 0 + m_CacheServerEndpoint: + m_CacheServerNamespacePrefix: default + m_CacheServerEnableDownload: 1 + m_CacheServerEnableUpload: 1 diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 4c21eea..a12731e 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1 +1,2 @@ -m_EditorVersion: 2018.4.14f1 +m_EditorVersion: 2019.4.10f1 +m_EditorVersionWithRevision: 2019.4.10f1 (5311b3af6f69)