Naev

Module rnd

Bindings for interacting with the random number generator.

This module not only allows basic random number generation, but it also handles more complicated statistical stuff.

Example usage would be:

 if rnd.rnd() < 0.5 then
    -- 50% chance of this happening
 else
    -- And 50% chance of this happening
 end
 

Functions

rnd (x, y) Gets a random number.
sigma () Creates a number in the one-sigma range [-1:1].
twosigma () Creates a number in the two-sigma range [-2:2].
threesigma () Creates a number in the three-sigma range [-3:3].
uniform (x, y) Gets a random number in the given range, with a uniform distribution.
angle () Gets a random angle, i.e., a random number from 0 to 2*pi.
permutation (input) Creates a random permutation


Functions

rnd (x, y)
Gets a random number. With no parameters it returns a random float between 0 and 1.

With one parameter it returns a whole number between 0 and that number (both included). With two parameters it returns a whole number between both parameters (both included).

Parameters:

  • x number First parameter, read description for details.
  • y number Second parameter, read description for details.

Returns:

    number A randomly generated number, read description for details.

Usage:

  • n = rnd() -- Number in range [0:1].
    
  • n = rnd(5) -- Number in range [0:5].
    
  • n = rnd(3,5) -- Number in range [3,5].
    
sigma ()
Creates a number in the one-sigma range [-1:1].

A one sigma range means that it creates a number following the normal distribution but limited to the 63% quadrant. This means that the number is biased towards 0, but can become either 1 or -1. It's a fancier way of generating random numbers.

Returns:

    number A number from [-1:1] biased slightly towards 0.

Usage:

    n = 5.5 + rnd.sigma()/2. -- Creates a number from 5 to 6 slightly
     biased to 5.5.
    
twosigma ()
Creates a number in the two-sigma range [-2:2].

This function behaves much like the rnd.sigma function but uses the two-sigma range, meaning that numbers are in the 95% quadrant and thus are much more random. They are biased towards 0 and approximately 63% will be within [-1:1]. The rest will be in either the [-2:-1] range or the [1:2] range.

Returns:

    number A number from [-2:2] biased heavily towards 0.

Usage:

    n = 5.5 + rnd.twosigma()/4. -- Creates a number from 5 to 6 heavily
    biased to 5.5.
    
threesigma ()
Creates a number in the three-sigma range [-3:3].

This function behaves much like its brothers rnd.sigma and rnd.twosigma. The main difference is that it uses the three-sigma range which is the 99% quadrant. It will rarely generate numbers outside the [-2:2] range (about 5% of the time) and create numbers outside of the [-1:1] range about 37% of the time. This can be used when you want extremes to appear rarely.

Returns:

    number A number from [-3:3] biased totally towards 0.

Usage:

    n = 5.5 + rnd.threesigma()/6. -- Creates a number from 5 to 6 totally
    biased to 5.5.
    
uniform (x, y)
Gets a random number in the given range, with a uniform distribution.

Parameters:

  • x number First parameter, read description for details.
  • y number Second parameter, read description for details.

Returns:

    number A randomly generated number, read description for details.

Usage:

  • n = uniform() -- Real number in the interval [0,1].
    
  • n = uniform(5) -- Real number in the interval [0,5].
    
  • n = uniform(3,5) -- Real number in the interval [3,5].
    
angle ()
Gets a random angle, i.e., a random number from 0 to 2*pi.

Returns:

    number A randomly generated angle, in radians.

Usage:

    vec2.newP(radius, rnd.angle())
    
permutation (input)
Creates a random permutation This creates a list from 1 to input and then randomly permutates it, however, if an ordered table is passed as a parameter, that is randomly permuted instead.

Parameters:

  • input number or table Maximum value to permutate to.

Returns:

    table A randomly permutated table.

Usage:

  • t = rnd.permutation( 5 )
    
  • t = rnd.permutation( {"cat", "dog", "cheese"} )
    
generated by LDoc 1.5.0 Last updated 2024-11-21 07:14:26