知识卡片

延伸参考卡 · 10641 · 生成模型:未记录

rand按区间生成猜题

理解等级:中级

在本书采用的 rand 0.8.5 示例中,thread_rng 返回当前执行线程使用的生成器。Rng 将区间采样方法带入可用作用域,gen_range(1..=100) 给猜数字程序提供 1 到 100 的答案。试运行可手工观察范围和流程,少量结果无法证明分布。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 猜数字游戏 Generating a Secret Number · 查看原书依据

      Next, we’re adding two lines in the middle. In the first line ❷, we call the rand::thread_rng function that gives us the particular random number generator we’re going to use: one that is local to the current thread of execution and is seeded by the operating system. Then we call the gen_range method on the random number generator.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第02章 猜数字游戏(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c02.xhtml`,块 0111) - 结论依据:书中说明线程局部生成器、Rng trait 和包含端点的区间。 - 原始内容:Next, we’re adding two lines in the middle. In the first line ❷, we call the rand::thread_rng function that gives us the particular random number generator we’re going to use: one that is local to the current thread of execution and is seeded by the operating system. Then we call the gen_range method on the random number generator.