라이브러리

[PHP] RandomRandomizer::nextInt - 양의 정수를 얻습니다.




PHP 에서 Randomizer 클래스는 PHP 8.0 버전부터 제공하는 기능입니다. 이 클래스는 난수 발생을 위한 다양한 메서드를 제공합니다. 여기서 Randomizer::nextInt() 메서드를 자세히 설명하겠습니다.

Randomizer::nextInt() 메서드


Randomizer::nextInt() 메서드는 0 이상의 정수를 반환하는 난수 발생 메서드입니다. 이 메서드는 주어진 범위 내의 정수를 반환합니다.

# 메서드 시그니처


#hostingforum.kr
php

public static function nextInt(int $min, int $max): int



# 매개변수


* `$min`: 반환할 난수의 최소값
* `$max`: 반환할 난수의 최대값

# 반환값


* 반환할 난수

예제


#hostingforum.kr
php

use Randomizer;



// 1부터 10까지의 난수를 발생

$randomNumber = Randomizer::nextInt(1, 10);

echo "난수: $randomNumber
";



// 50부터 100까지의 난수를 발생

$randomNumber = Randomizer::nextInt(50, 100);

echo "난수: $randomNumber
";



사용 예시


난수 발생을 위해 Randomizer::nextInt() 메서드를 사용할 수 있습니다. 예를 들어, 게임 개발에서 난수 발생을 사용할 수 있습니다.

#hostingforum.kr
php

// 게임 개발 예시

class Game {

    public function play() {

        // 1부터 10까지의 난수를 발생하여 게임의 난이도를 결정

        $difficulty = Randomizer::nextInt(1, 10);

        echo "난이도: $difficulty
";



        // 난수에 따라 게임의 결과를 결정

        if ($difficulty < 5) {

            echo "승리했습니다!
";

        } else {

            echo "패배했습니다...
";

        }

    }

}



$game = new Game();

$game->play();



참고


Randomizer::nextInt() 메서드는 PHP 8.0 버전부터 제공하는 기능입니다. PHP 버전이 낮은 경우, 다른 난수 발생 메서드를 사용해야 할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 300 페이지

검색

게시물 검색