• PHP Video Tutorials

PHP - Stats Rand Gen Funiform() Function



Definition and Usage

The stats_rand_gen_funiform() function can generate uniform float between low (exclusive) and high (exclusive).

Syntax

  float stats_rand_gen_funiform( float $low, float $high )

Parameters

Sr.No Parameter Description
1

low

The lower bound (inclusive)

2

high

The upper bound (exclusive)

Return Values

The stats_rand_gen_funiform() can return random deviate from a uniform distribution from low to high.

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 from the uniform distribution by passing $low < $high .

<?php
   $x = stats_rand_gen_funiform(1.5, 2.5);
   var_dump(is_float($x));
   var_dump($x > 1.5);
   var_dump($x < 2.5);
?>

Output

This will produce following result −

bool(true)
bool(true)
bool(true)

Example

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

<?php
   $x = stats_rand_gen_funiform(1.5, 1.5);
   var_dump($x);
?>

Output

This will produce following result −

float(1.5)

Example

Following is an error case. In the following example, we pass low > high. A warning is displayed in logs.

<?php
   // error cases
   var_dump(stats_rand_gen_funiform(2.5, 1.5));    // low > high
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_rand_gen_funiform(): low greater than high. low : 2.500000E+0 high : 1.500000E+0

bool(false)
Advertisements