Difference between revisions of "love.math.random"

(clarify codomain of functions)
m
(8 intermediate revisions by 6 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 way.
+
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.
 +
 
 
== Function ==
 
== Function ==
Get uniformly distributed pseudo random real number in [0,1].
+
Get uniformly distributed pseudo-random real number within [0, 1].
 +
 
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 10: Line 12:
 
None.
 
None.
 
=== 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 in [0,max]
+
Get a uniformly distributed pseudo-random '''integer''' within [1, max].
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 20: Line 23:
 
{{param|number|max|The maximum possible value it should return.}}
 
{{param|number|max|The maximum possible value it should return.}}
 
=== Returns ===
 
=== Returns ===
{{param|number|number|The pseudo random number.}}
+
{{param|number|number|The pseudo-random integer number.}}
 
== Function ==
 
== Function ==
Get uniformly distributed pseudo random integer in [min, max].
+
Get uniformly distributed pseudo-random '''integer''' within [min, max].
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 31: Line 34:
 
{{param|number|max|The maximum possible value it should return.}}
 
{{param|number|max|The maximum possible value it should return.}}
 
=== Returns ===
 
=== Returns ===
{{param|number|number|The pseudo random number.}}
+
{{param|number|number|The pseudo-random integer number.}}
  
 
== Examples ==
 
== Examples ==
Generates a number between 1 and 100 inclusive.
+
Generates a number between 1 and 100 (both inclusive).
 
<source lang="lua">
 
<source lang="lua">
 
function love.load()
 
function love.load()
Line 40: Line 43:
 
end
 
end
 
</source>
 
</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.randomnormal]]
+
* [[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}}

Revision as of 11:47, 29 July 2020

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.

Function

Get uniformly distributed pseudo-random real number within [0, 1].

Synopsis

number = love.math.random( )

Arguments

None.

Returns

number number
The pseudo-random number.

Function

Get a uniformly distributed pseudo-random integer within [1, max].

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 within [min, max].

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

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