I have simple script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DLATEST : MonoBehaviour
{
int wx = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
wx = Random.Range(-1, 1);
Debug.Log(wx);
}
}
but I have weird results: 0 -1 -2 -3 -4 -5 -6 -7 -8
wx slowly declines to negatives until it overflows.
I expect that wx will have values near 0 and slowly change around it, but not decline as it have. This is simple numerical integration but it works incorrectly.
I have also tried this code:
int wx = 0;
int wxPrev = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
wx = wxPrev Random.Range(-1, 1);
Debug.Log(wx);
wxPred = wx;
}
Results are the same.
If I debug Random.Range(-1, 1) only then everything is ok, random values are random.
What is wrong in my code? Thank you.
CodePudding user response:
If you use Random.Range(int x, int y) the result will be >= x && < y
In your case Random.Range(-1,1) always returns -1 or 0. You probably needed Random.Range(-1f,1f) - to force using Random.Range(float,float) instead of random int or change to Random.Range(-1,2) so it woult return integer -1, 0 or 1 randomly