logo

C의 무작위 함수

이번 주제에서는 난수 함수와 C 프로그래밍 언어에서 난수를 생성하는 방법에 대해 알아봅니다. 우리가 알고 있듯이, 무작위 함수는 정의된 두 숫자 사이의 난수를 찾는 데 사용됩니다. C 프로그래밍 언어에서 무작위 함수에는 rand() 및 srand() 함수라는 두 가지 내장 함수가 있습니다. C 언어에서 이러한 기능을 이해해 봅시다.

C의 무작위 함수

랜드() 함수

에서 C 프로그래밍 언어 , rand() 함수는 [0, RAND_MAX] 범위의 난수를 생성하는 라이브러리 함수입니다. 프로그램에서 rand() 함수를 사용할 때 다음을 구현해야 합니다. stdlib.h stdlib 헤더 파일에 rand() 함수가 정의되어 있기 때문입니다. 시드 번호가 포함되어 있지 않습니다. 따라서 동일한 프로그램을 반복해서 실행하면 동일한 값이 반환됩니다.

참고: srand() 함수를 호출하지 않고 rand() 함수를 사용하여 난수를 생성하면 프로그램이 실행될 때마다 동일한 일련의 숫자가 반환됩니다.

통사론

 int rand (void) 

rand() 함수는 범위가 0부터 RAND_MAX까지인 임의의 정수를 반환합니다. RAND_MAX는 stdlib.h 헤더 파일에 정의된 기호 상수로, C 라이브러리에 따라 그 값은 32767보다 크지만 32767보다 작습니다.

rand() 함수를 사용하여 난수 생성

rand() 함수를 사용하여 난수를 얻는 프로그램을 작성해 보겠습니다.

~이든

부분 파생 기호 라텍스
 #include #include #include void main() { // use rand() function to generate the number printf (' The random number is: %d', rand()); printf ('
 The random number is: %d', rand()); printf (' 
 The random number is: %d', rand()); printf ('
 The random number is: %d', rand()); getch(); } 

산출

 The random number is: 41 The random number is: 18467 The random number is: 6334 The random number is: 26500 

rand() 함수를 사용하여 5개의 난수 생성

C 프로그래밍 언어에서 rand() 함수를 사용하여 5개의 난수를 생성하는 프로그램을 고려해 보겠습니다.

무작위.c

 #include #include int main() { int i; /* It returns the same sequence of random number on every execution of the program. */ printf(' Random Numbers are: 
&apos;); for (i = 0; i <5; i++) { printf(' %d', rand()); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Random Numbers are: 41 18467 6334 26500 19169 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Random Numbers are: 41 18467 6334 26500 19169 </pre> <p> <strong>3rd execution of the program</strong> </p> <pre> Random Numbers are: 41 18467 6334 26500 19169 </pre> <p>As we can see in the above output, it returns the same sequence of random numbers on every execution of the programming code.</p> <h3>Generate 10 random numbers from 1 to 100 using rand() function</h3> <p>Let&apos;s consider a program to find the random number in C using rand() function.</p> <p> <strong>rand_num.c</strong> </p> <pre> #include #include #include int main() { // declare the local variables int i, num; printf (&apos; Program to get the random number from 1 to 100 
&apos;); for (i = 1; i <= 100 10; i++) { num="rand()" % + 1; use rand() function to get the random number printf (' %d ', num); getch(); } < pre> <p> <strong>Output</strong> </p> <pre> Program to get the random number from 1 to 100 42 68 35 1 70 25 79 59 63 65 </pre> <h2>srand() function</h2> <p>The srand() function is a C library function that determines the initial point to generate different series of pseudo-random numbers. A srand() function cannot be used without using a rand() function. The srand() function is required to set the value of the seed only once in a program to generate the different results of random integers before calling the rand() function.</p> <h3>Syntax</h3> <pre> int srand (unsigned int seed) </pre> <p> <strong>seed</strong> : It is an integer value that contains a seed for a new sequence of pseudo-random numbers.</p> <h3>Generate the random numbers using srand() function</h3> <p>Let&apos;s write a program to get the random numbers using srand() function in C.</p> <p> <strong>srandNum.c</strong> </p> <pre> #include #include #include // use time.h header file to use time int main() { int num, i; time_t t1; // declare time variable printf(&apos; Enter a number to set the limit for a random number 
&apos;); scanf (&apos; %d&apos;, &amp;num); /* define the random number generator */ srand ( (unsigned) time (&amp;t1)); // pass the srand() parameter printf(&apos;
&apos;); // print the space /* generate random number between 0 to 50 */ for (i = 0; i <num; i++) { printf( '%d 
', rand() % 50); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit for a random number 10 44 32 23 35 6 33 1 4 22 18 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter a number to set the limit for a random number 15 13 30 24 27 4 30 28 35 36 13 44 39 21 5 7 </pre> <p>As we can see in the above Output, it returns different sequences of random numbers on every execution of the programming code.</p> <h3>Generate the random numbers using srand() and time() function</h3> <p>Let&apos;s write a program to get the random numbers using srand() with time() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int random = rand(); // assign the rand() function to random variable srand( time(0)); printf( &apos; Seed = %d&apos;, time(0)); printf( &apos; Random number = %d&apos;, random); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Seed = 1619450091 Random number = 41 </pre> <h3>Get a seeding value and print the random numbers using srand() function</h3> <p>Let&apos;s write a program to get the seed value and random numbers using srand() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int count; unsigned int seed; // use for randomize number printf(&apos; Enter the Seeding value: 
&apos;); scanf(&apos; %u&apos;, &amp;seed); srand (seed); // pass parameter // generate random number between 1 to 6 for (count = 1; count <= 1 5="=" 10; ++count) { printf(' %5d', + (rand () % 6)); if (count 0) print the number in next line puts(' '); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the Seeding value: 10 6 4 3 3 6 3 3 1 3 4 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4 </pre> <p> <strong>3rd execution of the program:</strong> </p> <pre> Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3 </pre> <p>As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.</p> <h3>Generate the random number using the random function</h3> <p>Let&apos;s create a program to use stadlib header file to get the random number using random function in C.</p> <p> <strong>func.c</strong> </p> <pre> #include #include #include int main() { int i, num, max, temp; printf (&apos; Enter a number to set the limit of random numbers 
&apos;); scanf (&apos;%d&apos;, num); printf (&apos; Enter the maximum number from you want to get the random number: 
&apos;); scanf (&apos;%d&apos;, max); printf (&apos; %d random number from 0 to %d number are: 
&apos;, num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (' %d ', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let&apos;s consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: 
?); for (i = 0; i<10; i++) { printf('%f', ((float) rand() rand_max) * f1); printf('
'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=></pre></=></pre></num;></pre></=></pre></5;>

두 번째 프로그램 실행:

 Random Numbers are: 41 18467 6334 26500 19169 

3번째 프로그램 실행

 Random Numbers are: 41 18467 6334 26500 19169 

위 출력에서 ​​볼 수 있듯이 프로그래밍 코드를 실행할 때마다 동일한 일련의 난수를 반환합니다.

rand() 함수를 사용하여 1부터 100까지의 난수 10개 생성

rand() 함수를 사용하여 C에서 난수를 찾는 프로그램을 고려해 보겠습니다.

rand_num.c

 #include #include #include int main() { // declare the local variables int i, num; printf (&apos; Program to get the random number from 1 to 100 
&apos;); for (i = 1; i <= 100 10; i++) { num="rand()" % + 1; use rand() function to get the random number printf (\' %d \', num); getch(); } < pre> <p> <strong>Output</strong> </p> <pre> Program to get the random number from 1 to 100 42 68 35 1 70 25 79 59 63 65 </pre> <h2>srand() function</h2> <p>The srand() function is a C library function that determines the initial point to generate different series of pseudo-random numbers. A srand() function cannot be used without using a rand() function. The srand() function is required to set the value of the seed only once in a program to generate the different results of random integers before calling the rand() function.</p> <h3>Syntax</h3> <pre> int srand (unsigned int seed) </pre> <p> <strong>seed</strong> : It is an integer value that contains a seed for a new sequence of pseudo-random numbers.</p> <h3>Generate the random numbers using srand() function</h3> <p>Let&apos;s write a program to get the random numbers using srand() function in C.</p> <p> <strong>srandNum.c</strong> </p> <pre> #include #include #include // use time.h header file to use time int main() { int num, i; time_t t1; // declare time variable printf(&apos; Enter a number to set the limit for a random number 
&apos;); scanf (&apos; %d&apos;, &amp;num); /* define the random number generator */ srand ( (unsigned) time (&amp;t1)); // pass the srand() parameter printf(&apos;
&apos;); // print the space /* generate random number between 0 to 50 */ for (i = 0; i <num; i++) { printf( \'%d 
\', rand() % 50); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit for a random number 10 44 32 23 35 6 33 1 4 22 18 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter a number to set the limit for a random number 15 13 30 24 27 4 30 28 35 36 13 44 39 21 5 7 </pre> <p>As we can see in the above Output, it returns different sequences of random numbers on every execution of the programming code.</p> <h3>Generate the random numbers using srand() and time() function</h3> <p>Let&apos;s write a program to get the random numbers using srand() with time() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int random = rand(); // assign the rand() function to random variable srand( time(0)); printf( &apos; Seed = %d&apos;, time(0)); printf( &apos; Random number = %d&apos;, random); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Seed = 1619450091 Random number = 41 </pre> <h3>Get a seeding value and print the random numbers using srand() function</h3> <p>Let&apos;s write a program to get the seed value and random numbers using srand() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int count; unsigned int seed; // use for randomize number printf(&apos; Enter the Seeding value: 
&apos;); scanf(&apos; %u&apos;, &amp;seed); srand (seed); // pass parameter // generate random number between 1 to 6 for (count = 1; count <= 1 5="=" 10; ++count) { printf(\' %5d\', + (rand () % 6)); if (count 0) print the number in next line puts(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the Seeding value: 10 6 4 3 3 6 3 3 1 3 4 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4 </pre> <p> <strong>3rd execution of the program:</strong> </p> <pre> Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3 </pre> <p>As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.</p> <h3>Generate the random number using the random function</h3> <p>Let&apos;s create a program to use stadlib header file to get the random number using random function in C.</p> <p> <strong>func.c</strong> </p> <pre> #include #include #include int main() { int i, num, max, temp; printf (&apos; Enter a number to set the limit of random numbers 
&apos;); scanf (&apos;%d&apos;, num); printf (&apos; Enter the maximum number from you want to get the random number: 
&apos;); scanf (&apos;%d&apos;, max); printf (&apos; %d random number from 0 to %d number are: 
&apos;, num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (\' %d \', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let&apos;s consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: 
?); for (i = 0; i<10; i++) { printf(\'%f\', ((float) rand() rand_max) * f1); printf(\'
\'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=></pre></=></pre></num;></pre></=>

srand() 함수

srand() 함수는 다양한 계열의 의사 난수를 생성하기 위한 초기점을 결정하는 C 라이브러리 함수입니다. rand() 함수를 사용하지 않으면 srand() 함수를 사용할 수 없습니다. srand() 함수는 rand() 함수를 호출하기 전에 임의의 정수의 다른 결과를 생성하기 위해 프로그램에서 시드 값을 한 번만 설정해야 합니다.

통사론

 int srand (unsigned int seed) 

씨앗 : 새로운 의사 난수 시퀀스에 대한 시드를 포함하는 정수 값입니다.

srand() 함수를 사용하여 난수 생성

C에서 srand() 함수를 사용하여 난수를 얻는 프로그램을 작성해 보겠습니다.

srandNum.c

 #include #include #include // use time.h header file to use time int main() { int num, i; time_t t1; // declare time variable printf(&apos; Enter a number to set the limit for a random number 
&apos;); scanf (&apos; %d&apos;, &amp;num); /* define the random number generator */ srand ( (unsigned) time (&amp;t1)); // pass the srand() parameter printf(&apos;
&apos;); // print the space /* generate random number between 0 to 50 */ for (i = 0; i <num; i++) { printf( \'%d 
\', rand() % 50); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit for a random number 10 44 32 23 35 6 33 1 4 22 18 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter a number to set the limit for a random number 15 13 30 24 27 4 30 28 35 36 13 44 39 21 5 7 </pre> <p>As we can see in the above Output, it returns different sequences of random numbers on every execution of the programming code.</p> <h3>Generate the random numbers using srand() and time() function</h3> <p>Let&apos;s write a program to get the random numbers using srand() with time() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int random = rand(); // assign the rand() function to random variable srand( time(0)); printf( &apos; Seed = %d&apos;, time(0)); printf( &apos; Random number = %d&apos;, random); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Seed = 1619450091 Random number = 41 </pre> <h3>Get a seeding value and print the random numbers using srand() function</h3> <p>Let&apos;s write a program to get the seed value and random numbers using srand() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int count; unsigned int seed; // use for randomize number printf(&apos; Enter the Seeding value: 
&apos;); scanf(&apos; %u&apos;, &amp;seed); srand (seed); // pass parameter // generate random number between 1 to 6 for (count = 1; count <= 1 5="=" 10; ++count) { printf(\' %5d\', + (rand () % 6)); if (count 0) print the number in next line puts(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the Seeding value: 10 6 4 3 3 6 3 3 1 3 4 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4 </pre> <p> <strong>3rd execution of the program:</strong> </p> <pre> Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3 </pre> <p>As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.</p> <h3>Generate the random number using the random function</h3> <p>Let&apos;s create a program to use stadlib header file to get the random number using random function in C.</p> <p> <strong>func.c</strong> </p> <pre> #include #include #include int main() { int i, num, max, temp; printf (&apos; Enter a number to set the limit of random numbers 
&apos;); scanf (&apos;%d&apos;, num); printf (&apos; Enter the maximum number from you want to get the random number: 
&apos;); scanf (&apos;%d&apos;, max); printf (&apos; %d random number from 0 to %d number are: 
&apos;, num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (\' %d \', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let&apos;s consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: 
?); for (i = 0; i<10; i++) { printf(\'%f\', ((float) rand() rand_max) * f1); printf(\'
\'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=></pre></=></pre></num;>

두 번째 프로그램 실행:

 Enter a number to set the limit for a random number 15 13 30 24 27 4 30 28 35 36 13 44 39 21 5 7 

위 출력에서 ​​볼 수 있듯이 프로그래밍 코드를 실행할 때마다 서로 다른 난수 시퀀스를 반환합니다.

srand() 및 time() 함수를 사용하여 난수 생성

srand()와 time() 함수를 사용하여 난수를 얻는 프로그램을 작성해 보겠습니다.

srand_time.c

 #include #include int main() { int random = rand(); // assign the rand() function to random variable srand( time(0)); printf( &apos; Seed = %d&apos;, time(0)); printf( &apos; Random number = %d&apos;, random); return 0; } 

산출

 Seed = 1619450091 Random number = 41 

srand() 함수를 사용하여 시딩 값을 얻고 난수를 인쇄합니다.

srand() 함수를 사용하여 시드값과 난수를 구하는 프로그램을 작성해 보겠습니다.

srand_time.c

 #include #include int main() { int count; unsigned int seed; // use for randomize number printf(&apos; Enter the Seeding value: 
&apos;); scanf(&apos; %u&apos;, &amp;seed); srand (seed); // pass parameter // generate random number between 1 to 6 for (count = 1; count <= 1 5="=" 10; ++count) { printf(\' %5d\', + (rand () % 6)); if (count 0) print the number in next line puts(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the Seeding value: 10 6 4 3 3 6 3 3 1 3 4 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4 </pre> <p> <strong>3rd execution of the program:</strong> </p> <pre> Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3 </pre> <p>As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.</p> <h3>Generate the random number using the random function</h3> <p>Let&apos;s create a program to use stadlib header file to get the random number using random function in C.</p> <p> <strong>func.c</strong> </p> <pre> #include #include #include int main() { int i, num, max, temp; printf (&apos; Enter a number to set the limit of random numbers 
&apos;); scanf (&apos;%d&apos;, num); printf (&apos; Enter the maximum number from you want to get the random number: 
&apos;); scanf (&apos;%d&apos;, max); printf (&apos; %d random number from 0 to %d number are: 
&apos;, num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (\' %d \', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let&apos;s consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: 
?); for (i = 0; i<10; i++) { printf(\'%f\', ((float) rand() rand_max) * f1); printf(\'
\'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=></pre></=>

두 번째 프로그램 실행:

 Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4 

프로그램의 세 번째 실행:

 Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3 

위 출력에서 ​​볼 수 있듯이, 다른 시드 값을 사용하여 동일한 프로그램을 반복해서 실행하면 1부터 6까지의 서로 다른 난수 시퀀스가 ​​표시됩니다.

Random 함수를 사용하여 난수 생성

C에서 난수 함수를 사용하여 난수를 얻기 위해 stadlib 헤더 파일을 사용하는 프로그램을 만들어 보겠습니다.

func.c

 #include #include #include int main() { int i, num, max, temp; printf (&apos; Enter a number to set the limit of random numbers 
&apos;); scanf (&apos;%d&apos;, num); printf (&apos; Enter the maximum number from you want to get the random number: 
&apos;); scanf (&apos;%d&apos;, max); printf (&apos; %d random number from 0 to %d number are: 
&apos;, num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (\' %d \', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let&apos;s consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: 
?); for (i = 0; i<10; i++) { printf(\'%f\', ((float) rand() rand_max) * f1); printf(\'
\'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=>

부동 난수를 생성하는 프로그램

C에서 부동 소수점 난수를 인쇄하는 프로그램을 고려해 보겠습니다.

무작위1.c

 #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: 
?); for (i = 0; i<10; i++) { printf(\\'%f\\', ((float) rand() rand_max) * f1); printf(\\'
\\'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;>