Problem with random Range, returns always same number
well, as it says the title, ive made a small function to return a random vector3, but always returns the same values, not doing random. It always returns 0.1,0.1,0.1
void RandomVector(Vector3 a)
{
float uno = Random.Range(0.05f, 0.15f);
float dos = Random.Range(0.05f, 0.15f);
float tres = Random.Range(0.05f, 0.15f);
a = new Vector3(uno, dos, tres);
Debug.Log(a);
}
Answer by Obsessi0n · Feb 02, 2018 at 12:01 AM
The vector is correct the debug is just not showing the full float number change your debug for:
Debug.Log(a.ToString("F4"));
Answer by Ginxx009 · Feb 02, 2018 at 01:56 AM
In order not to repeat a number twice you might try storing the value of the first number. For example .
int [] array = new int[]{0,1,2,3,4,5,6};
List<int>list = null;
void Start(){
list.AddRange(array);
}
int GetUniqueRandom(bool reloadEmptyList){
if(list.Count == 0 ){
if(reloadEmptyList){
list.AddRange(array);
}
else{
return -1; // here is up to you
}
}
int rand = Random.Range(0, list.Count);
int value = list[rand];
list.RemoveAt(rand);
return value;
}
I hope it helps you.
Answer by world-is-torture · Feb 02, 2018 at 09:43 AM
It was a problem related to seeds and stuf, couldnt fix it, it always returned the same amount no matter what, but i found an easier way, ill put it here in case someone needs.
with only one sentence i could do all that shit.
vector3random = Random.insideUnitSphere * DistanciaMax;
Random.insideUniteSphere returns a random value inside a sphere of radius 1.
Are you saying that Obsession's answer is incorrect? I would expect the code you posted in the question to always log the vector as (0.1, 0.1, 0.1), simply because of the way you're logging it (all random numbers between 0.05 and 0.15 are 0.1 when displayed to 1 decimal place). It will not be always creating the same Vector3 though (unless you were to re-seed with the same number before each call).
Right, also the original code uses always a positive number for each component, so it will always be in the positive octant so only covering "1/8"th of the whole range. "insideUnitSphere" will return an vector in any direction.
Good point, plus the ranges in the OP start at a non-zero number, so it's a shell in that positive octant.
It's great that insideUnitSphere does what you want but it doesn't answer your original question and does something very different from what you've done in your original code. For the future if you have a problem you should describe the problem you want to solve. You seem to have choosen a solution and where stuck there. From your question it wasn't clear if you actually wanted a vector in the (+,+,+) octant or a generally random vector. Also your method "RandomVector" looks really strange as you pass in a vector as parameter which you replace inside the method. However since Vector3 is a value type the value you passed in won't be changed outside the method.
Your answer

Follow this Question
Related Questions
Random.Range not working? 2 Answers
Generate Random Numbers a Distance Apart From Each Other 1 Answer
Random.Range method "left-hand side" error 2 Answers
Random Range Without Duplicates 2 Answers