Difference between revisions of "love.math.random"
m |
(→Examples) |
||
(10 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{newin|[[0.9.0]]|090|type=function}} | {{newin|[[0.9.0]]|090|type=function}} | ||
− | Generates a pseudo-random number in a platform independent manner. | + | Generates a pseudo-random number in a platform independent manner. This function is seeded at startup, so you generally don't need to seed it yourself. |
+ | {{notice|Neither this function, [[RandomGenerator]], nor Lua [https://www.lua.org/manual/5.1/manual.html#pdf-math.random math.random] generates truly random number, thus it's unsuitable for cryptographic usage!}} | ||
+ | |||
== Function == | == Function == | ||
− | Get uniformly distributed pseudo-random real number | + | Get uniformly distributed pseudo-random '''real''' number between 0 inclusive to 1 exclusive. |
+ | |||
=== Synopsis === | === Synopsis === | ||
<source lang="lua"> | <source lang="lua"> | ||
Line 11: | Line 14: | ||
=== Returns === | === Returns === | ||
{{param|number|number|The pseudo-random number.}} | {{param|number|number|The pseudo-random number.}} | ||
+ | |||
== Function == | == Function == | ||
− | Get a uniformly distributed pseudo-random integer | + | Get a uniformly distributed pseudo-random '''integer''' between 1 inclusive to <code>max</code> inclusive. |
=== Synopsis === | === Synopsis === | ||
<source lang="lua"> | <source lang="lua"> | ||
Line 22: | Line 26: | ||
{{param|number|number|The pseudo-random integer number.}} | {{param|number|number|The pseudo-random integer number.}} | ||
== Function == | == Function == | ||
− | Get uniformly distributed pseudo-random integer | + | Get uniformly distributed pseudo-random '''integer''' between <code>min</code> inclusive to <code>max</code> inclusive. |
=== Synopsis === | === Synopsis === | ||
<source lang="lua"> | <source lang="lua"> | ||
Line 34: | Line 38: | ||
== Examples == | == Examples == | ||
− | Generates a number between 1 and 100 | + | Generates a number between 1 and 100 (both inclusive). |
<source lang="lua"> | <source lang="lua"> | ||
function love.load() | function love.load() | ||
Line 40: | Line 44: | ||
end | end | ||
</source> | </source> | ||
+ | |||
+ | Generate a random permutation of list: | ||
+ | <source lang="lua"> | ||
+ | function shuffle (list) | ||
+ | -- backward iteration from last to second element: | ||
+ | for i = #list, 2, -1 do | ||
+ | -- choose one of elements: | ||
+ | local j = love.math.random(i) -- between 1 to i (both inclusive) | ||
+ | -- replace both elements each other: | ||
+ | list[i], list[j] = list[j], list[i] | ||
+ | end | ||
+ | end | ||
+ | </source> | ||
+ | |||
+ | == Notes == | ||
+ | When using the 2nd and 3rd variant, numbers passed will be rounded, thus, <code>love.math.random(0, 76.767)</code> may return 77 | ||
== See Also == | == See Also == | ||
* [[parent::love.math]] | * [[parent::love.math]] | ||
* [[love.math.setRandomSeed]] | * [[love.math.setRandomSeed]] | ||
− | * [[love.math. | + | * [[love.math.randomNormal]] |
* [[love.math.newRandomGenerator]] | * [[love.math.newRandomGenerator]] | ||
[[Category:Functions]] | [[Category:Functions]] | ||
{{#set:Description=Get uniformly distributed pseudo-random number}} | {{#set:Description=Get uniformly distributed pseudo-random number}} | ||
+ | |||
== Other Languages == | == Other Languages == | ||
{{i18n|love.math.random}} | {{i18n|love.math.random}} |
Latest revision as of 12:24, 23 March 2023
Available since LÖVE 0.9.0 |
This function is not supported in earlier versions. |
Generates a pseudo-random number in a platform independent manner. This function is seeded at startup, so you generally don't need to seed it yourself.
Neither this function, RandomGenerator, nor Lua math.random generates truly random number, thus it's unsuitable for cryptographic usage! |
Contents
Function
Get uniformly distributed pseudo-random real number between 0 inclusive to 1 exclusive.
Synopsis
number = love.math.random( )
Arguments
None.
Returns
number number
- The pseudo-random number.
Function
Get a uniformly distributed pseudo-random integer between 1 inclusive to max
inclusive.
Synopsis
number = love.math.random( max )
Arguments
number max
- The maximum possible value it should return.
Returns
number number
- The pseudo-random integer number.
Function
Get uniformly distributed pseudo-random integer between min
inclusive to max
inclusive.
Synopsis
number = love.math.random( min, max )
Arguments
number min
- The minimum possible value it should return.
number max
- The maximum possible value it should return.
Returns
number number
- The pseudo-random integer number.
Examples
Generates a number between 1 and 100 (both inclusive).
function love.load()
randomNumber = love.math.random(1, 100)
end
Generate a random permutation of list:
function shuffle (list)
-- backward iteration from last to second element:
for i = #list, 2, -1 do
-- choose one of elements:
local j = love.math.random(i) -- between 1 to i (both inclusive)
-- replace both elements each other:
list[i], list[j] = list[j], list[i]
end
end
Notes
When using the 2nd and 3rd variant, numbers passed will be rounded, thus, love.math.random(0, 76.767)
may return 77
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info