Global/VectorRand

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Origin.)
Line 11: Line 11:
 
| Returns = {{Return|Vector|randVec}}
 
| Returns = {{Return|Vector|randVec}}
 
}}
 
}}
 
+
==Definition==
==Replacement==
+
<pre>return Vector( math.Rand(-1.0, 1.0), math.Rand(-1.0, 1.0), math.Rand(-1.0, 1.0) )</pre>
{{Example
+
| Description = The following replacement for VectorRand will produce uniformly distributed unit vectors (directions)
+
| Code = function VectorRandDir()
+
    local theta = math.Rand(0,2*math.pi)
+
    local radius = math.sqrt(math.Rand(0,1))
+
    return Vector(radius * math.cos(theta), radius * math.sin(theta), math.sqrt(1-radius*radius) * (math.random(0,1)*2-1))
+
end
+
}}
+
  
 
==Additional Notes==
 
==Additional Notes==
* This will not return unit vectors (Vectors with length = 1) and normalizing it will result in vectors biased towards the corners of a cube, rather than uniform distribution
+
* This will not return unit vectors (Vectors with length = 1), use {{ClassFunction|Vector|GetNormal}} on this

Revision as of 01:45, 24 September 2012

Template:Function

Template:Syntax

Definition

return Vector( math.Rand(-1.0, 1.0), math.Rand(-1.0, 1.0), math.Rand(-1.0, 1.0) )

Additional Notes

  • This will not return unit vectors (Vectors with length = 1), use Vector:GetNormal on this
Personal tools
Navigation