Quaternion random rotation issue

I need to trigger random rotation of an object, but within some limits. Basically, I want to keep it in front of another object. What I’ve used is a function like the one below:

void RandomRotate(){

		float randomX = Random.Range (20,140);
		float randomY = Random.Range (0,90);
		float randomZ = Random.Range (60,300);

		Quaternion randomRotation = Quaternion.Euler (randomX, randomY, randomZ);

		targetObject.transform.rotation = randomRotation;


, which for the time being I’ve been triggering with a simple spacebar keystroke. This function is meant to be called multiple times in my game. The thing is, that some cases I don’t get the correct values in transform.rotation, but get a relative value to 180 and the random value instead. I print my random values in my console and compare them to the transform.rotation values in the inspector. Here is some values I get:

1st stroke:
randomX=57, randomY=78, randomZ=161
transform.rotation in the inspector: X=56.9999 Y=78.0001 Z=161

2nd stroke:
randomX=106, randomY=41, randomZ=68
transform.rotation in the inspector: X=74 (180-106), Y=221 (180+41), Z=248 (180+68)

3rd stroke:
randomX=125, randomY=56, randomZ=118
transform.rotation in the inspector: X=55 (180-125), Y=236 (180+56), Z=298 (180+118)

4th stroke:
randomX=49, randomY=71, randomZ=115
transform.rotation in the inspector: X=49, Y=71.0001, Z=115

5th stroke:
randomX=106, randomY=16, randomZ=123
transform.rotation in the inspector: X=74.0002 (180-74), Y=196 (180+16), Z=303 (180+123)


The occurrence of this seems to be completely random, but it messes up my intended rotation. Is there any explanation and solution?

Thanks in advance

rotations are tricky and im not quite sure what it is that you are trying to do here , but unity flips and rotates things relative to 180 in the inspector not sure why it does this for instance try when in play mode , rotate an object on the x axis to 180 degrees you should see the other rotations change to 180 and back again and your x axis should only allow you to go to 90 degrees before getting stuck there , not sure why it does this could be a bug , but after this rotate it back and you will see the object continue rotating around , wish i could be of more help but thats just the way it is .