Random::randint

Return random integers from low (inclusive) to high (exclusive).

Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is null (the default), then results are from [0, low).

sciphp/numphp>=0.5

Description

SciPhp\NdArray Random::randint( int $low , int $high , int|array $size )

Parameters

$low
An integer. Lowest (signed) integer to be drawn from the distribution (unless high=null, in which case this parameter is one above the highest such integer).
$high
An integer.

This parameter is optional.

If provided, one above the largest (signed) integer to be drawn from the distribution.
$size
An array of positive integers[1, 2, 3] or an integer.

This parameter is optional.

Output shape. If the given shape is, e.g., [m, n, k], then m * n * k samples are drawn. Default is null, in which case a single value is returned.

Return Values

int or a NdArray of ints size-shaped array of random integers from the appropriate distribution, or a single such random int if size not provided.

Examples

Example #1: Get a single random integer between 0 and 5

use SciPhp\NumPhp as np;

$x np::random()->randint(6);

echo 
$x;

The above example will output:

3

Example #2: Get a single random integer between -5 and 5

use SciPhp\NumPhp as np;

$x np::random()->randint(-56);

echo 
$x;

The above example will output:

x
-2

Example #3: Get a 1-dim matrix with 10 random integers between -10 and 10

use SciPhp\NumPhp as np;

$x np::random()->randint(-101110);

echo 
$x;

The above example will output:

[ 1    5   -8   -3   -1   -6    6    4   -8    7 ]

Example #4: Get a 4x2 matrix with random integers between -10 and 10

use SciPhp\NumPhp as np;

$x np::random()->randint(-1011, [42]);

echo 
$x;

The above example will output:

[[-2   -2 ]
 [ 7   -3 ]
 [ 5    3 ]
 [-2    0 ]]

Example #5: Use the random generated matrice with another one

x is the random generated matrice, y a triangular 4x4 and z=x.y

use SciPhp\NumPhp as np;

$x np::tri(4);

$y np::random()->randint(-1011, [44]);

$z $x->dot($y);

echo 
"x\n$x\n""y\n$y\n""z=x.y\n$z\n";

The above example will output:

x
[[1  0  0  0]
 [1  1  0  0]
 [1  1  1  0]
 [1  1  1  1]]

y
[[-5    -10   -10    7  ]
 [-10    9    -6     5  ]
 [ 0     8    -1     10 ]
 [-2    -8     7     9  ]]

z=x.y
[[-5    -10   -10    7  ]
 [-15   -1    -16    12 ]
 [-15    7    -17    22 ]
 [-17   -1    -10    31 ]]

See Also