Revision Difference
table.Random#511831
<function name="Random" parent="table" type="libraryfunc">⤶
<description>⤶
Returns a random value from the supplied table.⤶
⤶
<note>This function iterates over the given table **twice**, therefore with sequential tables you should instead use following:⤶
⤶
```⤶
mytable[ math.random( #mytable ) ]⤶
```⤶
</note>⤶
</description>⤶
<realm>Shared and Menu</realm>⤶
<file line="158">lua/includes/extensions/table.lua</file>⤶
<args>⤶
<arg name="haystack" type="table">The table to choose from.</arg>⤶
</args>⤶
<rets>⤶
<ret name="" type="any">A random value from the table.</ret>⤶
<ret name="" type="any">The key associated with the random value.</ret>⤶
</rets>⤶
</function>⤶
⤶
<example>⤶
<description>A simple example of this function using two tables.</description>⤶
<code>⤶
color = { "green", "red", "blue", "yellow" }⤶
object = { "car", "house", "bike" }⤶
⤶
print( "I have a " .. table.Random( color ) .. " " .. table.Random( object ) .. "." )⤶
</code>⤶
<output>I have a green house.</output>⤶
⤶
</example>⤶
⤶
⤶
<example>⤶
<description>Example of using the alternative with sequential tables for performance reasons.</description>⤶
<code>⤶
websites = {"facepunch.com", "google.com", "steampowered.com"}⤶
print("I think the best website ever is " .. websites[math.random(1, #websites)] .. ".")⤶
</code>⤶
<output>I think the best website ever is google.com.</output>⤶
⤶
</example>