logo

כיצד ליצור מספר אקראי בין 1 ל-10 ב-C++

יצירת מספרים אקראיים היא דרישה נפוצה ביישומי תכנות רבים, ו-C++ מספק מספר דרכים ליצור מספרים אקראיים בטווח נתון. במאמר זה, נחקור שיטות שונות ליצירת מספרים אקראיים בין 1 ל-10 ב-C++.

שיטה 1:

שימוש בפונקציה rand():

אחת השיטות הפשוטות ביותר ליצור מספר אקראי בין 1 ל-10 ב-C++ היא רנד() פוּנקצִיָה. פונקציה זו מוגדרת ב- קובץ header ויוצר מספר שלם אקראי בטווח של 0 ל RAND_MAX . הערך של RAND_MAX הוא תלוי יישום ויכול להשתנות מקומפיילר למהדר.

דוגמא:

ניקח דוגמה ליצירת מספר אקראי בין 1 ל-10 באמצעות הפונקציה rand(), נוכל להשתמש בקוד הבא:

 #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;>

בקוד זה, כללנו את ו קבצי כותרות. ה srand() הפונקציה משמשת לאתחול מחולל המספרים האקראיים עם הזמן הנוכחי בתור הזרע. זה מבטיח שבכל פעם שהתוכנית מופעלת, נוצר רצף חדש של מספרים אקראיים.

ה רנד() הפונקציה משמשת ליצירת מספר שלם אקראי בין 0 ל RAND_MAX . כדי להגביל את הטווח שבין 1 ל-10, ניקח את יתרת המספר הזה בחלוקה ב-10 ונוסיף לו 1.

שיטה 2:

שימוש בספריית C++11 אקראית

ה C++11 standard הציג ספרייה חדשה בשם זה מספק דרך טובה יותר ליצור מספרים אקראיים. ספרייה זו מספקת מספר מנועים והפצות לייצור מספרים אקראיים שיכולים ליצור מספרים אקראיים עם הפצה אחידה.

דוגמא:

ניקח דוגמה ליצירת מספר אקראי בין 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;>

בקוד זה, השתמשנו ב- זְמַן() פונקציה כדי לקבל את השעה הנוכחית כערך ראשוני עבור srand() פוּנקצִיָה. ה srand() הפונקציה משמשת לאתחול מחולל המספרים האקראיים. ה רנד() הפונקציה יוצרת מספר שלם אקראי בין 0 ל RAND_MAX , אשר אז מוגבל לטווח שבין 1 ל-10 באמצעות ה- מודול מפעיל ומוסיפים לו 1.

סיכום:

לסיכום, ישנן מספר שיטות ליצור מספרים אקראיים בין 1 ל-10 ב-C++. בחירת השיטה תלויה בדרישות האפליקציה, כגון מהירות, אקראיות , ו אֲחִידוּת של מספרים שנוצרו. בזמן ש רנד() הפונקציה היא הפשוטה והקלה ביותר ליישום, ייתכן שהיא לא תספק אקראיות ואחידות טובה. ה הספרייה מספקת דרך טובה יותר ליצור מספרים אקראיים עם התפלגות אחידה, אך היא איטית ומורכבת יותר ליישום. ה XORShift אלגוריתם מספק טוב אקראיות ו אֲחִידוּת , אבל זה מורכב יותר ליישום ואולי לא יהיה מהיר כמו ה רנד() פוּנקצִיָה.