Global/VectorRand

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Origin.)
 
(7 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Function
+
{{Func
| Name = VectorRand
+
|Name=VectorRand
| Namespace = [[Global]]
+
|Parent=Global
| Realm = Shared
+
|Description=Returns a random vector whose components are each between min(inclusive), max(exclusive).
| Origin = Lua
+
|Realm=Shared and Menu
 +
|IsClass=No
 +
|File=lua/includes/util.lua
 +
|Line=94-L98
 
}}
 
}}
 
+
{{Arg
{{Syntax
+
|type=number
| Name = VectorRand
+
|name=min
| Description = Returns a random vector whose components are each between -1 and 1
+
|default=-1
| Returns = {{Return|Vector|randVec}}
+
|desc=Min bound inclusive.
 
}}
 
}}
 
+
{{Arg
==Replacement==
+
|type=number
{{Example
+
|name=max
| Description = The following replacement for VectorRand will produce uniformly distributed unit vectors (directions)
+
|default=1
| Code = function VectorRandDir()
+
|desc=Max bound exclusive.
    local theta = math.Rand(0,2*math.pi)
+
}}
    local radius = math.sqrt(math.Rand(0,1))
+
{{Ret
    return Vector(radius * math.cos(theta), radius * math.sin(theta), math.sqrt(1-radius*radius) * (math.random(0,1)*2-1))
+
|type=Vector
end
+
|desc=The random direction vector.
 
}}
 
}}
 
==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
 

Latest revision as of 17:44, 2 July 2019

 VectorRand( )

Contents

Description

Returns a random vector whose components are each between min(inclusive), max(exclusive).

Arguments

number min=-1

Min bound inclusive.

Arguments

number max=1

Max bound exclusive.

Returns

Vector

The random direction vector.
Personal tools
Navigation