Global/VectorRand

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Created page with "{{Function | Name = VectorRand | Namespace = Global | Realm = Shared }} {{Syntax | Name = VectorRand | Description = Returns a random vector whose components are each bet...")
 
m (Origin.)
Line 3: Line 3:
 
| Namespace = [[Global]]
 
| Namespace = [[Global]]
 
| Realm = Shared
 
| Realm = Shared
 +
| Origin = Lua
 
}}
 
}}
  

Revision as of 19:06, 23 September 2012

Template:Function

Template:Syntax

Contents

Replacement

Examples

Example

The following replacement for VectorRand will produce uniformly distributed unit vectors (directions)

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

  • 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
Personal tools
Navigation