>  기사  >  백엔드 개발  >  PHP의 난수 생성기

PHP의 난수 생성기

王林
王林원래의
2024-08-29 13:13:13701검색

이 글에서는 PHP의 난수 생성기에 대해 알아 보겠습니다. 그렇다면 난수 생성기는 무엇인가요?

내장된 함수를 사용하여 난수나 정수를 생성할 수 있습니다. 이 기능은 무엇을 합니까? min 및 max 범위 내의 이러한 함수는 서로 다른 숫자 세트를 생성합니다. 그리고 이 함수를 호출할 때마다 고유한 숫자가 생성됩니다. 2자리 숫자, 3자리 숫자 등 어떤 숫자든 생성할 수 있습니다.

광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

숫자는 범위 내에서 섞이고 그에 따라 생성됩니다. 난수를 생성하는 다양한 내장 함수가 있습니다.

난수 생성기 기능

이제 의사 난수를 생성하는 다양한 함수에 대해 알아 보겠습니다.

  • 범위가 없는 rand() 함수, 범위가 있는 rand() 함수: 이 함수는 호출되면 난수를 반환합니다. 함수에 min과 max를 입력하면 범위 내에서 난수를 생성합니다.
  • mt_rand() 함수: 이 함수는 rand()와 유사합니다. mt_rand()의 mt는 Mersenne Twister를 나타냅니다. mt_rand() 함수는 난수 생성기이며 정수 값을 반환합니다. rand() 함수와 마찬가지로 의사 난수를 생성합니다. 이는 최초의 의사 난수 생성기였습니다. 이는 기존 난수 생성기의 고급 형태입니다. 빠르고 효율적이며 고품질 정수를 제공합니다.
  • getrandmax() 함수: 이 함수에는 정의된 매개 변수가 없으며 이름에서 알 수 있듯이 가능한 최대 난수를 반환합니다.
  • mt_getrandmax() 함수: getrandmax() 함수와 유사하며 가능한 최대 난수를 반환합니다. 여기서도 mt는 난수 생성 알고리즘인 Mersenne Twister의 약자입니다.
  • srand(seed) 함수: 이 함수가 제공되지 않은 경우 이 함수는 난수 생성기에 주어진 시드 값을 시드합니다.
  • mt_srand(seed): 이 함수는 srand() 함수와 유사하며 이 함수는 주어진 시드 값을 난수 생성기에 시드합니다.

언급된 각 함수 유형의 예와 구문을 학습합니다.

1. 랜드() 함수

구문:

rand()

예:

<?php
// program to generate random integer value
echo '<br>'.'Following are the different random values';
echo '<hr/>';
echo '<br>'. rand();
echo '<hr/>';
echo '<br>'. rand();
echo '<hr/>';
echo '<br>'. rand();
?>

출력:

PHP의 난수 생성기

2. 주어진 범위 내의 rand() 함수

이 함수는 rand() 함수에 범위를 제공합니다.

구문:

rand(min, max);

여기서 min은 선택적 최소값으로 가장 낮은 숫자 값을 나타내고, max는 선택적 최대값으로 가장 높은 숫자 값을 나타냅니다.

또한 min의 기본값은 0이고 max의 기본값은 getrandmax() 함수 값입니다. 함수의 반환 유형은 항상 정수입니다.

예:

<?php
// program to generate random integer value
echo 'Following are the different random values within ranges min and max';
echo '<hr/>';
echo '<br> Range : 1 to 100 ----> '. rand(1,100);
echo '<hr/>';
echo '<br> Range 5 to 25 ---->'. rand(5, 25);
echo '<hr/>';
echo '<br>Range 10000 to 50000 --->'. rand(10000, 50000);
?>

출력:

PHP의 난수 생성기

3. mt_rand() 함수

구문:

int mt_rand(min, max)

여기서 min은 선택적 값으로 가장 낮은 숫자를 나타내고, max는 선택적 값으로 가장 높은 숫자를 나타냅니다. min의 기본값은 0이고 max의 기본값은 주어진 가장 높은 값입니다. 반환 유형은 정수입니다.

예:

<?php
// program to generate random integer value
echo 'Following are the different random values using mt_rand()';
echo '<hr/>';
echo '<br> Range : 1 to 100 ----> '. mt_rand(1,100);
echo '<hr/>';
echo '<br> Range 5 to 25 ---->'. mt_rand(5, 25);
echo '<hr/>';
echo '<br>Range 9 to 19 --->'. mt_rand(9, 19);
?>

출력:

PHP의 난수 생성기

4. getrandmax() 함수

구문:

mt_getrandmax();

이 함수는 정수 값을 반환합니다

:

<?php
// program to generate random integer values
//using getrandmax() function
echo 'Random number using getrandmax() function';
echo '<hr/>';
echo(getrandmax());
echo '<hr>';
?>

출력:

PHP의 난수 생성기

5. mt_getrandommax() 함수

구문:

mt_getrandmax();

이 함수는 정수 값을 반환합니다.

예:

<?php
// program to generate random integer values
//using mt_getrandmax() function
echo 'random number using mt_getrandmax() function';
echo '<hr/>';
echo(mt_getrandmax());
?>

출력:

PHP의 난수 생성기

6. srand() 함수

구문:

srand(seed);

시드가 선택적 값인 경우 이 함수는 아무것도 반환하지 않습니다.

예:

<?php
// program to generate random integer value
echo 'example using srand';
echo '<br>'. srand(3);
echo(rand(1, 5));
echo '<hr>';
echo 'example using srand';
echo '<br>'. srand(2);
echo(rand(1, 5));
?>

출력:

PHP의 난수 생성기

7. mt_srand() Function

Example:

<?php
// program to generate random integer value using mt_srand() function
echo 'example using mt_srand';
echo '<hr>';
mt_srand(5);
echo mt_rand(1,5);
?>

Output:

PHP의 난수 생성기

Generation Integers

In the following example we have used rand(),rand(min,max) and mt_rand().

Code:

<?php
// program to generate random integer value
echo 'Following are the different random values';
echo '<br> Any random number ---->'. rand();
echo '<br> Any random number ---->'. rand();
echo '<hr>';
// random number with range
echo 'Following are the different random values within a range ';
echo '<br> Any random number within the range from 0 to 9----> '. rand(0,9);
echo '<br>Any random number within the range from 1000  to 9999 ---->'. rand(1000,9999);
echo '<hr>';
// random number with range
echo 'Following are the different random values using mt_rand() ';
echo '<br> Using mt_rand()---->'. mt_rand(1000,9999);
echo '<br> Using mt_rand()---->'. mt_rand(100,999);
?>

Output:

PHP의 난수 생성기

Generation Floating-Point Numbers

Floating-point numbers represent a number with decimals that are of the type float. Examples – 10.0, 8.12, 6.23e-5, 2.345, 2.98e+10 and more.

Code:

<?php
function fun($min, $max) {
$square_root = sqrt(4);
return mt_rand($min * $square_root, $max * $square_root) /  100;
}
echo 'Program to display floating point numbers ';
echo '<hr>';
echo "<br>".fun(1, 10, 2);
?>

Output:

PHP의 난수 생성기

Conclusion

In this article, we learned about various functions used to generate a random number in PHP. These functions are explained with sample examples. Hope this article is found useful to anyone who wants to learn a random number generator in PHP.

위 내용은 PHP의 난수 생성기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP base64_decode다음 기사:PHP base64_decode