# Random.Range producing values outside its range.

I’ve just started unity and I made a basic 2D game that I then converted to 3D, using the same code just reskinning some things, doing this, despite using the same code, my random.range spawned objects in the 120-degree area I had it working for, but in 3D this seems to be anywhere on the 360 degrees area.

As far as I’m aware there are no other cases of this happening I can find online.

The number generator:

``````void RotRange (float startRot, float endRot){

Vector3 rotation = transform.eulerAngles;

rotation.z = Mathf.Clamp (Random.Range (startRot,endRot), startRot, endRot);

print (rotation.z + " , " + pattenNum);

//print (rotation.z);

transform.eulerAngles = rotation;
}
``````

(startRot is 0f, endRot is 120f)

But even when I use mathf.clamp the values are still on a 360 spectrum and not been confined so i think there must be some external factor, I’m not sure if anyone knows what might cause this, and there is pretty limited information to go on but thank you in advance I am surprised this is not giving errors like you have it. try like this:

``````void RotRange (float startRot, float endRot){
float f;
f = UnityEngine.Random.Range (startRot,endRot);
Vector3 rotation = new Vector3(transform.eulerAngles.x,
transform.eulerAngles.y,
f);
print (rotation.z + " , " + pattenNum);

transform.eulerAngles = rotation;
}
``````

in C# you cant assign just one coordinate to a Vector3. You have to assign the whole thing.
you can read one at a time though.