• PHP Video Tutorials

PHP - Stats Rand Gen Beta() Function



Definition and Usage

The stats_rand_gen_beta() function can generate a random deviate from the beta distribution.

Syntax

  float stats_rand_gen_beta( float $a, float $b )

Parameters

Sr.No Parameter Description
1

a

The shape parameter of the beta distribution

2

b

The shape parameter of the beta distribution

Return Values

The stats_rand_gen_beta() function can return a random deviate from the beta distribution with parameters A and B. The density of the beta is x^(a-1) * (1-x)^(b-1) / B(a,b) for 0 < x <. Method R. C. H. Cheng.

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 the beta distribution .

<?php
   var_dump(is_float(stats_rand_gen_beta(2, 3)));
?>

Output

This will produce following result −

bool(true)

Example

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

<?php
   // error cases
   var_dump(stats_rand_gen_beta(1e-38, 1));    // a < 1e-37
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_rand_gen_beta(): 'a' or 'b' lower than 1.0E-37. 'a' value : 1.000000E-38 'b' value : 1.000000E+0

bool(false)

Example

Following is an error case. In the following example, we pass b < 1e-37. A warning is displayed in logs.

<?php
   // error cases
   var_dump(stats_rand_gen_beta(1, 1e-38));    // b < 1e-37
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_rand_gen_beta(): 'a' or 'b' lower than 1.0E-37. 'a' value : 1.000000E+0 'b' value : 1.000000E-38

bool(false)
Advertisements