• PHP Video Tutorials

PHP - Stats Rand Gen Iuniform() Function



Definition and Usage

The stats_rand_gen_iuniform() function can generate an integer uniformly distributed between LOW (inclusive) and HIGH (inclusive).

Syntax

  int stats_rand_gen_iuniform( int $low, int $high )

Parameters

Sr.No Parameter Description
1

low

The lower bound

2

high

The upper bound

Return Values

The stats_rand_gen_iuniform() function can return a random integer from the discrete uniform distribution between low (inclusive) and high (inclusive).

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 an integer uniformly distributed between 1 and 3 (low < high).

<?php
   var_dump(stats_rand_gen_iuniform(1, 3));
?>

Output

This will produce following result −

int(2)

Example

In the following example, we generate an integer uniformly distributed between 1 and 1 (low == high.).

<?php
   var_dump(stats_rand_gen_iuniform(1, 1));
?>

Output

This will produce following result −

int(0)

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_iuniform(2, 1));    // low > high
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_rand_gen_iuniform(): low greater than high. low :2 high: 1

bool(false)
Advertisements