I created a class for generating random values based on weights per value as follows:
public struct RandomizableField<T>
{
public T value;
public float weight;
public RandomizableField(T value, float weight)
{
this.value = value;
this.weight = weight;
}
}
public class RandomizableSet<T>
{
public List<RandomizableField<T>> set;
public T GetRandomValue()
{
float totalWeight = 0.0f;
float[] valueWeight = new float[set.Count];
for(int i = 0; i < set.Count; i++)
{
valueWeight *= totalWeight;*
_ totalWeight += set*.weight;_
_ }*_
* float randomValue = Random.Range(0.0f, totalWeight);*
* for(int i = set.Count - 1; i >= 0; i–)*
* {*
_ if(randomValue >= valueWeight*)
{
return set.value;
}
}
return default(T); // Fallback in case List is empty*
* }*_
* public RandomizableSet(List<RandomizableField> values)*
* {*
* set = values;*
* }*
}
I’m currently utilizing this class to define values such as this (edited to save space in this post):
public static RS randomFloat =
new RS(new List<RF>()
{
* new RF(1.0f, 50.0f),*
* new RF(2.0f, 15.0f),*
* new RF(3.0f, 10.0f),*
* new RF(Random.Range(1.0f, 8.0f), 25.0f)*
});
To clarify, this generates one of the following:
1.0 (50% of the time)
2.0 (15% of the time)
3.0 (10% of the time)
Random between 1.0 and 8.0 (25% of the time)
When I generate this as a static variable, it only calculates the randomized value among them a single time upon script initialization. Therefore, my question is this:
Is there any (reasonable) way to randomize that randomized variable every time I call it as-is, or would that absolutely require type-specific variants of this generic class in order to support that?