Random Range#
Random / Random Range
Can Read
Cannot Write
Description#
Returns a random value between two values, both included
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
using Random = UnityEngine.Random;
namespace VisualScript.Runtime
{
[Get]
[Title("Random Range")]
[Category("Random/Range")]
[Description("Returns a random value between two values, both included")]
[Image(typeof(IconDice), ColorTheme.Type.Yellow)]
[Keywords("Dice")]
[Serializable]
public class TypeNumberRandomRange : TypeNumber
{
[SerializeField] private GetNumber m_Min;
[SerializeField] private GetNumber m_Max;
public override double GetPreview(GameObject source) =>
Random.Range(
(float) this.m_Min.GetPreview(source),
(float) this.m_Max.GetPreview(source)
);
public TypeNumberRandomRange()
{
this.m_Min = new GetNumber(0f);
this.m_Max = new GetNumber(1f);
}
public TypeNumberRandomRange(float min, float max)
{
this.m_Min = new GetNumber(min);
this.m_Max = new GetNumber(max);
}
public override double Get(Args args)
{
float min = this.m_Min.GetFloat(args);
float max = this.m_Max.GetFloat(args);
return Random.Range(min, max);
}
public override string ToString()
{
return $"Random({this.m_Min}, {this.m_Max})";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnUpdate;
}
}