protected function buildRandomIntegers($count, $max = null, $excludes = [], $unique = true)
    {
        $max = $max ?: $count;

        // If it should not be unique do the simple task
        if (!$unique) {
            $numbers = [];
            for ($i = 0; $i < $count; ++$i) {
                $numbers[] = rand(0, $max - 1);
            }

            return $numbers;
        }

        if ($count > $max && $unique) {
            throw new InvalidArgumentException("An array with $count unique values with more items than max ($max) is impossible");
        }

        // Generate numbers until max
        $numbers = range(0, $max - 1);

        // Remove all excludes from the numbers
        foreach ($excludes as $exclude) {
            if (isset($numbers[$exclude])) {
                unset($numbers[$exclude]);
            }
        }

        // If the numbers are less than count, add the excludes back until count

        $i = 0;
        while (count($numbers) < $count) {
            $numbers[] = $excludes[$i];
            ++$i;
        }

        shuffle($numbers);

        $slice = [];

        for ($i = 0, $n = 0; $i < $count; ++$i) {
            $slice[] = array_pop($numbers);
        }

        return $slice;
    }
protected function buildRandomIntegers($count, $max = null, $excludes = [], $unique = true)
    {
        $max = $max ?: $count;

        // If it should not be unique do the simple task
        if (!$unique) {
            $numbers = [];
            for ($i = 0; $i < $count; ++$i) {
                $numbers[] = rand(0, $max - 1);
            }

            return $numbers;
        }

        if ($count > $max && $unique) {
            throw new InvalidArgumentException("An array with $count unique values with more items than max ($max) is impossible");
        }

        // Generate numbers until max
        $numbers = range(0, $max - 1);

        // Remove all excludes from the numbers
        foreach ($excludes as $exclude) {
            if (isset($numbers[$exclude])) {
                unset($numbers[$exclude]);
            }
        }

        // If the numbers are less than count, add the excludes back until count

        $i = 0;
        while (count($numbers) < $count) {
            $numbers[] = $excludes[$i];
            ++$i;
        }

        shuffle($numbers);

        $slice = [];

        for ($i = 0, $n = 0; $i < $count; ++$i) {
            $slice[] = array_pop($numbers);
        }

        return $slice;
    }
protected function buildRandomIntegers ( $ count , $ max = null , $ excludes = [ ] , $ unique = true ) { $ max = $ max ? : $ count ; // If it should not be unique do the simple task if ( ! $ unique ) { $ numbers = [ ] ; for ( $ i = 0 ; $ i < $ count ; ++ $ i ) { $ numbers [ ] = rand ( 0 , $ max - 1 ) ; } return $ numbers ; } if ( $ count > $ max && $ unique ) { throw new InvalidArgumentException ( "An array with $count unique values with more items than max ($max) is impossible" ) ; } // Generate numbers until max $ numbers = range ( 0 , $ max - 1 ) ; // Remove all excludes from the numbers foreach ( $ excludes as $ exclude ) { if ( isset ( $ numbers [ $ exclude ] ) ) { unset ( $ numbers [ $ exclude ] ) ; } } // If the numbers are less than count, add the excludes back until count $ i = 0 ; while ( count ( $ numbers ) < $ count ) { $ numbers [ ] = $ excludes [ $ i ] ; ++ $ i ; } shuffle ( $ numbers ) ; $ slice = [ ] ; for ( $ i = 0 , $ n = 0 ; $ i < $ count ; ++ $ i ) { $ slice [ ] = array_pop ( $ numbers ) ; } return $ slice ; }
Create some random integers.

@param int  $count
@param int  $max    (optional)
@param bool $unique (optional)

@return array
Create some random integers .