Skip to content

Commit

Permalink
docs: 补充 C 随机数的完整示例
Browse files Browse the repository at this point in the history
  • Loading branch information
FeignClaims authored Dec 5, 2024
1 parent e8df516 commit b1ccc50
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions faq/generate_random_number/main.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ C 随机数
}
}
综上, 正确的方式是:

1. 在程序开始时用 :cpp:`srand` 设置随机数种子为当前时间;
2. 之后按你的需求使用 `rand` 获取随机数.

.. code-block:: cpp
:linenos:
int main () {
srand(time(nullptr));
for (int i = 0; i < 10; ++i) {
cout << (rand() % 10 + 5);
}
}
------------------------------------------------------------------------------------------------------------------------
随机浮点数
------------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit b1ccc50

Please sign in to comment.