Garry's Mod Wiki

table.Random

any, any table.Random( table haystack )

Description

Returns a random value from the supplied table.

This function iterates over the given table twice, therefore with sequential tables you should instead use following:

mytable[ math.random( #mytable ) ]

Arguments

1 table haystack
The table to choose from.

Returns

1 any
A random value from the table.
2 any
The key associated with the random value.

Example

A simple example of this function using two tables.

color = { "green", "red", "blue", "yellow" } object = { "car", "house", "bike" } print( "I have a " .. table.Random( color ) .. " " .. table.Random( object ) .. "." )
Output: I have a green house.

Example

Example of using the alternative with sequential tables for performance reasons.

websites = {"facepunch.com", "google.com", "steampowered.com"} print("I think the best website ever is " .. websites[math.random(1, #websites)] .. ".")
Output: I think the best website ever is google.com.

Page Links


Special Pages


Wikis

?

Render Time: 31ms

DB GetPage 4
Generate Html 5
SaveChanges (1) 8
Render Body 0
Render Sidebar 12