##NOTE - This function comes with a bit of an optimization cost since it has to do more math than normal, but it is easy to use. Make a new/unique function if you need to run this A *LOT* every tick.


#If it has the tag, reset its rotation
execute as @s[tag=ResetRot] run data modify entity @s Rotation[0] set value 0.0f


#If it has a custom rotation value set, tag it. Reset scoreboard if needed.
tag @s[scores={KyuRotDummy1=-100000..}] add HasRotParameter
scoreboard players set @s[scores={KyuRotDummy1=-9000000}] KyuRotDummy1 0


#If a custom rotation parameter is not set, set it to 180(degrees).
execute as @s[tag=!HasRotParameter] run scoreboard players set @s KyuRotDummy1 180


#If not tagged to obtain a fresh UUID, take its own UUID.
execute as @s[tag=!GetFreshUUID] run execute at @s run execute store result score @s KyuRotDummy2 run data get entity @s UUID[0] .1


#If it is tagged to obtain a fresh UUID and not use its own, summon a marker and take its UUID.
execute as @s[tag=GetFreshUUID] run summon marker ~ ~ ~ {Tags:["FreshUUID"]}
execute as @s[tag=GetFreshUUID] run execute at @s run execute store result score @s KyuRotDummy2 run data get entity @e[type=marker,distance=..2,tag=FreshUUID,limit=1,sort=nearest] UUID[0] .1
execute as @s[tag=GetFreshUUID] run kill @e[type=marker,distance=..2,tag=FreshUUID,limit=1,sort=nearest]


#Divide UUID by rotation parameter to recieve a random value that does not exceed the parameter.
scoreboard players operation @s KyuRotDummy2 %= @s KyuRotDummy1


#50 percent chance to make our number negative instead
execute if predicate suhc:50percent run tag @s add DoNegative
execute as @s[tag=DoNegative] run scoreboard players set @s KyuRotDummy4 -1
execute as @s[tag=DoNegative] run scoreboard players operation @s KyuRotDummy2 *= @s KyuRotDummy4




#If a parameter was set beforehand, do some math to subtract half of the max value from the random number, to shift the range of values
#execute as @s[tag=HasRotParameter] run scoreboard players operation @s KyuRotDummy3 = @s KyuRotDummy1
#execute as @s[tag=HasRotParameter] run scoreboard players set @s KyuRotDummy4 2
#execute as @s[tag=HasRotParameter] run scoreboard players operation @s KyuRotDummy3 /= @s KyuRotDummy4
#execute as @s[tag=HasRotParameter] run scoreboard players operation @s KyuRotDummy3 /= @s KyuRotDummy4
#execute as @s[tag=HasRotParameter] run scoreboard players operation @s KyuRotDummy2 -= @s KyuRotDummy3


#If parameter was not set beforehand, subtract 180(half of the max) to shift the range of values (-180 to 180)
#execute as @s[tag=!HasRotParameter] run scoreboard players remove @s KyuRotDummy2 180






#Get current rotation, then apply our random value to it.
execute store result score @s KyuRotDummy4 run data get entity @s Rotation[0] 1
scoreboard players operation @s KyuRotDummy4 += @s KyuRotDummy2
execute store result entity @s Rotation[0] float 1 run scoreboard players get @s KyuRotDummy4


#reset tags and scores
tag @s remove HasRotParameter
tag @s remove GetFreshUUID
tag @s remove DoNegative
tag @s remove ResetRot
scoreboard players set @s KyuRotDummy1 -9000000
scoreboard players set @s KyuRotDummy2 0
scoreboard players set @s KyuRotDummy3 0
scoreboard players set @s KyuRotDummy4 0



