logo

C++에서 1에서 10 사이의 난수를 생성하는 방법

난수 생성은 많은 프로그래밍 응용 프로그램에서 일반적인 요구 사항이며 C++에서는 주어진 범위 내에서 난수를 생성하는 여러 가지 방법을 제공합니다. 이 기사에서는 C++에서 1에서 10 사이의 난수를 생성하는 다양한 방법을 살펴보겠습니다.

방법 1:

rand() 함수 사용:

C++에서 1에서 10 사이의 난수를 생성하는 가장 간단한 방법 중 하나는 랜드() 기능. 이 함수는 헤더 파일을 생성하고 다음 범위 내에서 임의의 정수를 생성합니다. 0 에게 RAND_MAX . 의 가치 RAND_MAX 구현에 따라 다르며 컴파일러마다 다를 수 있습니다.

예:

rand() 함수를 사용하여 1에서 10 사이의 난수를 생성하는 예를 들어 보겠습니다. 다음 코드를 사용할 수 있습니다.

 #include #include #include using namespace std; int main() { srand(time(0)); cout&lt;&lt; &apos;Random number between 1 and 10 is: &apos;&lt;<endl; for(int i="0;i&lt;10;i++)" cout << (rand() % 10) + 1<<' '; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Random number between 1 and 10 is: 4 5 7 10 7 5 1 7 10 2 </pre> <p>In this code, we have included the <strong> <em></em> </strong> and <strong> <em></em> </strong> header files. The <strong> <em>srand()</em> </strong> function is used to initialize the random number generator with the current time as the seed. It ensures that every time the program is run, a new sequence of random numbers is generated.</p> <p>The <strong> <em>rand()</em> </strong> function is used to generate a random integer between 0 and <strong> <em>RAND_MAX</em> </strong> . To limit the range between 1 and 10, we take the remainder of this number when divided by 10 and add 1 to it.</p> <h3>Method 2:</h3> <p> <strong>Using C++11 random library</strong> </p> <p>The <strong> <em>C++11</em> </strong> standard introduced a new library called <strong> <em></em> </strong> that provides a better way to generate random numbers. This library provides several random number generation engines and distributions that can generate random numbers with a uniform distribution.</p> <p> <strong>Example:</strong> </p> <p>Let&apos;s take an example to generate a random number between 1 and 10 using the <strong> <em></em> </strong> library, we can use the following code:</p> <pre> #include #include using namespace std; int main() { random_device rand; mt19937 gen(rand()); uniform_int_distributiondis(1, 10); int random_number = dis(gen); cout&lt;&lt; &apos;Random number between 1 and 10 is: &apos; &lt;<random_number<<endl; return 0; } < pre> <p>In this code, we have included the <strong> <em></em> </strong> header file. The <strong> <em>random_device</em> </strong> class is used to obtain a seed value for the random number generator. The <strong> <em>mt19937</em> </strong> class is a random number generation engine that produces random numbers with a uniform distribution. The <strong> <em>uniform_int_distribution</em> </strong> class is used to generate random integers within a given range.</p> <p>By default, the <strong> <em>mt19937</em> </strong> engine uses a seed value of <strong> <em>5489</em> </strong> , which can be changed using the <strong> <em>seed()</em> </strong> method. However, it is recommended to use a <strong> <em>random_device</em> </strong> to obtain a seed value for better randomness.</p> <p>The <strong> <em>uniform_int_distribution</em> </strong> class generates random integers with a uniform distribution within a given range. In this code, we have specified the range as <strong> <em>1</em> </strong> to <strong> <em>10</em> </strong> using the constructor.</p> <p>This method provides better randomness and a uniform distribution of generated numbers compared to the <strong> <em>rand()</em> </strong> function. However, it is slower and more complex to implement.</p> <h3>Method 3:</h3> <p> <strong>Using modulo operator with time():</strong> </p> <p>Another method to generate a random number between 1 and 10 is the <strong> <em>modulo operator</em> </strong> with the current time as a seed value. This method is similar to the first method using <strong> <em>rand()</em> </strong> function, but it uses a more random seed value and provides better randomness.</p> <p> <strong>Example:</strong> </p> <p>Let&apos;s take an example to generate a random number between 1 and 10 using the modulo operator with <strong> <em>time()</em> </strong> , we can use the following code:</p> <pre> #include #include using namespace std; int main() { srand(time(0)); cout&lt;&lt; &apos;Random number between 1 and 10 is: &apos; &lt;<endl; for(int i="0;i&lt;10;i++)" cout << (rand() % 10) + 1<<' '; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Random number between 1 and 10 is: 6 6 3 6 10 10 1 7 6 4 </pre> <p>In this code, we have used the <strong> <em>time()</em> </strong> function to obtain the current time as a seed value for the <strong> <em>srand()</em> </strong> function. The <strong> <em>srand()</em> </strong> function is used to initialize the random number generator. The <strong> <em>rand()</em> </strong> function generates a random integer between 0 and <strong> <em>RAND_MAX</em> </strong> , which is then limited to a range between 1 and 10 using the <strong> <em>modulo operator</em> </strong> and adding 1 to it.</p> <h2>Conclusion:</h2> <p>In conclusion, there are several methods to generate random numbers between 1 and 10 in C++. The choice of method depends on the requirements of the application, such as <strong> <em>speed, randomness</em> </strong> , and <strong> <em>uniformity</em> </strong> of generated numbers. While the <strong> <em>rand()</em> </strong> function is the simplest and easiest to implement, it may not provide good randomness and uniformity. The <strong> <em></em> </strong> library provides a better way to generate random numbers with a uniform distribution, but it is slower and more complex to implement. The <strong> <em>XORShift</em> </strong> algorithm provides good <strong> <em>randomness</em> </strong> and <strong> <em>uniformity</em> </strong> , but it is more complex to implement and may not be as fast as the <strong> <em>rand()</em> </strong> function.</p> <hr></endl;></pre></random_number<<endl;></pre></endl;>

이 코드에는 다음이 포함되었습니다. 그리고 헤더 파일. 그만큼 샌드() 함수는 현재 시간을 시드로 사용하여 난수 생성기를 초기화하는 데 사용됩니다. 프로그램이 실행될 때마다 새로운 난수 시퀀스가 ​​생성됩니다.

그만큼 랜드() 함수는 0과 사이의 임의의 정수를 생성하는 데 사용됩니다. RAND_MAX . 1에서 10 사이의 범위를 제한하기 위해 이 숫자를 10으로 나눈 나머지 값에 1을 더합니다.

방법 2:

C++11 무작위 라이브러리 사용

그만큼 C++11 표준에는 다음과 같은 새로운 라이브러리가 도입되었습니다. 이는 난수를 생성하는 더 나은 방법을 제공합니다. 이 라이브러리는 균일한 분포로 난수를 생성할 수 있는 여러 난수 생성 엔진과 분포를 제공합니다.

예:

다음을 사용하여 1에서 10 사이의 난수를 생성하는 예를 들어 보겠습니다. 라이브러리에서 다음 코드를 사용할 수 있습니다.

 #include #include using namespace std; int main() { random_device rand; mt19937 gen(rand()); uniform_int_distributiondis(1, 10); int random_number = dis(gen); cout&lt;&lt; &apos;Random number between 1 and 10 is: &apos; &lt;<random_number<<endl; return 0; } < pre> <p>In this code, we have included the <strong> <em></em> </strong> header file. The <strong> <em>random_device</em> </strong> class is used to obtain a seed value for the random number generator. The <strong> <em>mt19937</em> </strong> class is a random number generation engine that produces random numbers with a uniform distribution. The <strong> <em>uniform_int_distribution</em> </strong> class is used to generate random integers within a given range.</p> <p>By default, the <strong> <em>mt19937</em> </strong> engine uses a seed value of <strong> <em>5489</em> </strong> , which can be changed using the <strong> <em>seed()</em> </strong> method. However, it is recommended to use a <strong> <em>random_device</em> </strong> to obtain a seed value for better randomness.</p> <p>The <strong> <em>uniform_int_distribution</em> </strong> class generates random integers with a uniform distribution within a given range. In this code, we have specified the range as <strong> <em>1</em> </strong> to <strong> <em>10</em> </strong> using the constructor.</p> <p>This method provides better randomness and a uniform distribution of generated numbers compared to the <strong> <em>rand()</em> </strong> function. However, it is slower and more complex to implement.</p> <h3>Method 3:</h3> <p> <strong>Using modulo operator with time():</strong> </p> <p>Another method to generate a random number between 1 and 10 is the <strong> <em>modulo operator</em> </strong> with the current time as a seed value. This method is similar to the first method using <strong> <em>rand()</em> </strong> function, but it uses a more random seed value and provides better randomness.</p> <p> <strong>Example:</strong> </p> <p>Let&apos;s take an example to generate a random number between 1 and 10 using the modulo operator with <strong> <em>time()</em> </strong> , we can use the following code:</p> <pre> #include #include using namespace std; int main() { srand(time(0)); cout&lt;&lt; &apos;Random number between 1 and 10 is: &apos; &lt;<endl; for(int i="0;i&lt;10;i++)" cout << (rand() % 10) + 1<<\' \'; return 0; } < pre> <p> <strong>Output</strong> </p> <pre> Random number between 1 and 10 is: 6 6 3 6 10 10 1 7 6 4 </pre> <p>In this code, we have used the <strong> <em>time()</em> </strong> function to obtain the current time as a seed value for the <strong> <em>srand()</em> </strong> function. The <strong> <em>srand()</em> </strong> function is used to initialize the random number generator. The <strong> <em>rand()</em> </strong> function generates a random integer between 0 and <strong> <em>RAND_MAX</em> </strong> , which is then limited to a range between 1 and 10 using the <strong> <em>modulo operator</em> </strong> and adding 1 to it.</p> <h2>Conclusion:</h2> <p>In conclusion, there are several methods to generate random numbers between 1 and 10 in C++. The choice of method depends on the requirements of the application, such as <strong> <em>speed, randomness</em> </strong> , and <strong> <em>uniformity</em> </strong> of generated numbers. While the <strong> <em>rand()</em> </strong> function is the simplest and easiest to implement, it may not provide good randomness and uniformity. The <strong> <em></em> </strong> library provides a better way to generate random numbers with a uniform distribution, but it is slower and more complex to implement. The <strong> <em>XORShift</em> </strong> algorithm provides good <strong> <em>randomness</em> </strong> and <strong> <em>uniformity</em> </strong> , but it is more complex to implement and may not be as fast as the <strong> <em>rand()</em> </strong> function.</p> <hr></endl;></pre></random_number<<endl;>

이 코드에서는 시간() 현재 시간을 시드 값으로 얻는 함수 샌드() 기능. 그만큼 샌드() 함수는 난수 생성기를 초기화하는 데 사용됩니다. 그만큼 랜드() 함수는 0과 사이의 임의의 정수를 생성합니다. RAND_MAX , 다음을 사용하여 1에서 10 사이의 범위로 제한됩니다. 연산자 모듈 그리고 거기에 1을 더합니다.

결론:

결론적으로 C++에서는 1에서 10 사이의 난수를 생성하는 방법이 여러 가지가 있습니다. 방법 선택은 다음과 같은 애플리케이션 요구 사항에 따라 달라집니다. 속도, 무작위성 , 그리고 일률 생성된 숫자의 동안 랜드() 함수는 가장 간단하고 구현하기 쉽지만 좋은 임의성과 균일성을 제공하지 못할 수 있습니다. 그만큼 라이브러리는 균일 분포로 난수를 생성하는 더 나은 방법을 제공하지만 구현이 더 느리고 복잡합니다. 그만큼 XORShift 알고리즘은 좋은 것을 제공합니다 무작위성 그리고 일률 하지만 구현하기가 더 복잡하고 속도가 빠르지 않을 수 있습니다. 랜드() 기능.