• PHP Video Tutorials

PHP - Stats Rand Gen Funiform() Function



Definition and Usage

The stats_rand_gen_gamma() function can generate a random deviate from the gamma distribution.

Syntax

  float stats_rand_gen_gamma( float $a, float $r )

Parameters

Sr.No Parameter Description
1

a

location parameter of Gamma distribution (a > 0).

2

r

shape parameter of Gamma distribution (r > 0).

Return Values

The stats_rand_gen_gamma() function can generate a random deviate from the gamma distribution whose density is (A**R)/Gamma(R) * X**(R-1) * Exp(-A*X).

Dependencies

This function was first introduced in statistics extension (PHP 4.0.0 and PEAR 1.4.0). We have used latest release of stats-2.0.3 (PHP 7.0.0 or newer and PEAR 1.4.0 or newer) for this tutorial

Example

In the following example, we compute random deviation from gamma distribution.

<?php
   var_dump(is_float(stats_rand_gen_gamma(1, 1)));
?>

Output

This will produce following result −

bool(true)

Example

Following is an error case. In the following example, we pass a < 0. A warning is displayed in logs.

<?php
   // error case
   var_dump(stats_rand_gen_gamma(-0.1, 1));    // a < 0
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_rand_gen_gamma(): A or R nonpositive. A value : -1.000000E-1, R value : 1.000000E+0

bool(false)

Example

Following is an error case. In the following example, we pass a == 0. A warning is displayed in logs.

<?php
   // error cases
   var_dump(stats_rand_gen_gamma(0, 1));       // a == 0
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_rand_gen_gamma(): A or R nonpositive. A value : 0.000000E+0, R value : 1.000000E+0

bool(false)

Example

Following is an error case. In the following example, we pass r < 0. A warning is displayed in logs.

<?php
   // error cases
   var_dump(stats_rand_gen_gamma(1, -.1));     // r < 0
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_rand_gen_gamma(): A or R nonpositive. A value : 1.000000E+0, R value : -1.000000E-1

bool(false)

Example

Following is an error case. In the following example, we pass r == 0. A warning is displayed in logs.

<?php
   // error cases
   var_dump(stats_rand_gen_gamma(1, 0));       // r == 0
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_rand_gen_gamma(): A or R nonpositive. A value : 1.000000E+0, R value : 0.000000E+0

bool(false)
Advertisements