Вход Регистрация
Файл: vendor/fakerphp/faker/src/Faker/Core/Number.php
Строк: 106
<?php

declare(strict_types=1);

namespace 
FakerCore;

use 
FakerExtension;

/**
 * @experimental This class is experimental and does not fall under our BC promise
 */
final class Number implements ExtensionNumberExtension
{
    public function 
numberBetween(int $min 0int $max 2147483647): int
    
{
        
$int1 min($min$max);
        
$int2 max($min$max);

        return 
mt_rand($int1$int2);
    }

    public function 
randomDigit(): int
    
{
        return 
mt_rand(09);
    }

    public function 
randomDigitNot(int $except): int
    
{
        
$result self::numberBetween(08);

        if (
$result >= $except) {
            ++
$result;
        }

        return 
$result;
    }

    public function 
randomDigitNotZero(): int
    
{
        return 
mt_rand(19);
    }

    public function 
randomFloat(?int $nbMaxDecimals nullfloat $min 0, ?float $max null): float
    
{
        if (
null === $nbMaxDecimals) {
            
$nbMaxDecimals $this->randomDigit();
        }

        if (
null === $max) {
            
$max $this->randomNumber();

            if (
$min $max) {
                
$max $min;
            }
        }

        if (
$min $max) {
            
$tmp $min;
            
$min $max;
            
$max $tmp;
        }

        return 
round($min mt_rand() / mt_getrandmax() * ($max $min), $nbMaxDecimals);
    }

    public function 
randomNumber(int $nbDigits nullbool $strict false): int
    
{
        if (
null === $nbDigits) {
            
$nbDigits $this->randomDigitNotZero();
        }
        
$max 10 ** $nbDigits 1;

        if (
$max mt_getrandmax()) {
            throw new 
InvalidArgumentException('randomNumber() can only generate numbers up to mt_getrandmax()');
        }

        if (
$strict) {
            return 
mt_rand(10 ** ($nbDigits 1), $max);
        }

        return 
mt_rand(0$max);
    }
}
Онлайн: 1
Реклама