Last active
November 10, 2021 14:10
-
-
Save cosmogonies/8698138 to your computer and use it in GitHub Desktop.
Deterministic random manager, where you can dump into your code a random array (to freeze it), with also the option to have it neutral (average=median value).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
/* | |
Deterministic random manager, where you can dump into your code a random array (to freeze it), | |
with also the option to have it neutral (average=median value). | |
*/ | |
public class RandomSequence | |
{ //Auxiliary function to dump into code a random sequence, to freeze it, control it. | |
public int maximumCapacity; | |
public bool isNeutral; | |
internal bool isEven; | |
internal float medianValue; | |
internal float[] data; | |
public RandomSequence(int _Size, float _Min=-1.0f , float _Max=1.0f ,bool _Neutral=true) | |
{ | |
this.isNeutral = _Neutral; | |
this.maximumCapacity = _Size; | |
this.medianValue = (_Min + (_Max-_Min)*0.5f); | |
this.isEven = (this.maximumCapacity %2) == 0 ; | |
data = new float[this.maximumCapacity]; | |
if(_Neutral) | |
{ | |
int midIndex; | |
//Debug.Log("midIndex for"+ this.maximumCapacity+" is ="+midIndex); | |
if(this.isEven) | |
midIndex = Mathf.RoundToInt( this.maximumCapacity*0.5f ); | |
else | |
midIndex = Mathf.RoundToInt( (this.maximumCapacity-1)*0.5f ); | |
//midIndex --; //in Odd number, RoundToInt returns allways the even one. 11/2 => 6 13/2 => 6 | |
List<float> RandBuffer = new List<float>(); | |
for (int i = 0; i < midIndex ; i++) | |
{ | |
data[i] = UnityEngine.Random.Range(_Min,_Max); | |
RandBuffer.Add( data[i] ); | |
} | |
if( ! isEven) | |
{ | |
data[midIndex] = medianValue; | |
midIndex ++; | |
} | |
for (int i = midIndex; i < this.maximumCapacity ; i++) | |
{ | |
data[i] = RandBuffer[ UnityEngine.Random.Range(0,RandBuffer.Count) ] ; | |
RandBuffer.Remove( data[i] ); | |
data[i] *= -1; | |
} | |
if( RandBuffer.Count>0) | |
Debug.LogWarning ( "There is still ("+RandBuffer.Count+") random values in data mirror." ); | |
} | |
else | |
{ | |
for (int i = 0; i < this.maximumCapacity; i++) | |
data[i] = UnityEngine.Random.Range(_Min,_Max); | |
} | |
} | |
override public string ToString() | |
{ | |
//string FormattedBuffer ="float[] data = new float["+maximumCapacity+"] {"; | |
string FormattedBuffer ="new float["+maximumCapacity+"] {"; | |
for (int i = 0; i < maximumCapacity; i++) | |
{ | |
if(data[i].ToString().Length>5 ) | |
FormattedBuffer += data[i].ToString().Substring(0,5) + "f," ; //let's keep 3 digits only. | |
else | |
FormattedBuffer += data[i].ToString() + "f," ; | |
} | |
return FormattedBuffer+"};"; | |
} | |
} | |
#region UnitaryTest | |
public static class RandomSequence_UnitaryTest | |
{ | |
public static bool doIt() | |
{ | |
//RandomSequence rs = new RandomSequence (512, -1.0f, 1.0f, true); | |
RandomSequence rs = new RandomSequence (6, -1.0f, 1.0f, true); | |
Debug.Log (rs.ToString ()); | |
Debug.Log ( checkNeutrality(rs) ); | |
rs = new RandomSequence (512, -1.0f, 1.0f, true); | |
Debug.Log (rs.ToString ()); | |
Debug.Log ( checkNeutrality(rs) ); | |
rs = new RandomSequence (11, -1.0f, 1.0f, true); | |
Debug.Log (rs.ToString ()); | |
Debug.Log ( checkNeutrality(rs) ); | |
rs = new RandomSequence (13, -1.0f, 1.0f, true); | |
Debug.Log (rs.ToString ()); | |
Debug.Log ( checkNeutrality(rs) ); | |
rs = new RandomSequence (101, 1.0f, 3.0f, true); | |
Debug.Log (rs.ToString ()); | |
Debug.Log ( checkNeutrality(rs) ); | |
rs = new RandomSequence (101, 1.0f, 3.0f, false); | |
Debug.Log (rs.ToString ()); | |
Debug.Log ( checkNeutrality(rs) ); | |
return true; | |
} | |
public static bool checkNeutrality( RandomSequence _current) | |
{ | |
float sum = 0; | |
for (int i = 0; i < _current.data.Length; i++) | |
sum += _current.data[i]; | |
Debug.Log ("Sum is "+sum); | |
if (_current.isNeutral) | |
{ | |
//if (sum - _current.medianValue < Mathf.Epsilon) | |
if (sum - _current.medianValue < 0.00001f) //let's tolerate approximations... | |
return true; | |
else | |
return false; | |
} | |
else | |
return true; | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment