• PHP Video Tutorials

PHP - Stats Rand Gen Normal() Function



Definition and Usage

The stats_rand_gen_normal() function can generate a single random deviate from a normal distribution.

Syntax

  float stats_rand_gen_normal( float $av, float $sd )

Parameters

Sr.No Parameter Description
1

av

php_stats_rand_gen_normal_function

2

sd

The standard deviation of the normal distribution

Return Values

The stats_rand_gen_normal() function can return a random deviate from the normal distribution with mean, av, and standard deviation, sd.

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 generate a random deviate from the normal distribution with mean, 0, and standard deviation, 1.

<?php
   var_dump(is_float(stats_rand_gen_normal(0, 1)));
?>

Output

This will produce following result −

bool(true)

Example

In the following example, we generate a random deviate from the normal distribution with mean, 0, and standard deviation, 0.

<?php
   var_dump(stats_rand_gen_normal(0, 0));
?>

Output

This will produce following result −

 float(0)

Example

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

<?php
   // error cases
   var_dump(stats_rand_gen_normal(0, -0.1)); // sd < 0
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_rand_gen_normal(): sd < 0.0 . sd : -1.000000E-1

bool(false)
Advertisements