簡體   English   中英

如何在 Rust 1.0 中獲得隨機數?

[英]How can I get a random number in Rust 1.0?

我試過

use std::rand::{task_rng, Rng};

fn main() {
    // a number from [-40.0, 13000.0)
    let num: f64 = task_rng().gen_range(-40.0, 1.3e4);
    println!("{}", num);
}

但這給

error[E0432]: unresolved import `std::rand::task_rng`
 --> rand.rs:1:17
  |
1 | use std::rand::{task_rng, Rng};
  |                 ^^^^^^^^ no `task_rng` in `rand`

error[E0432]: unresolved import `std::rand::Rng`
 --> rand.rs:1:27
  |
1 | use std::rand::{task_rng, Rng};
  |                           ^^^ no `Rng` in `rand`

error[E0603]: module `rand` is private
 --> rand.rs:1:17
  |
1 | use std::rand::{task_rng, Rng};
  |                 ^^^^^^^^

error[E0603]: module `rand` is private
 --> rand.rs:1:27
  |
1 | use std::rand::{task_rng, Rng};
  |                           ^^^

我試過

extern crate rand;
use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    if rng.gen() {
        // random bool
        println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
    }
    let tuple = rand::random::<(f64, char)>();
    println!("{:?}", tuple)
}

並得到

error[E0425]: cannot find function `thread_rng` in module `rand`
 --> rand.rs:5:29
  |
5 |         let mut rng = rand::thread_rng();
  |                             ^^^^^^^^^^ not found in `rand`
  |
help: possible candidate is found in another module, you can import it into scope
  |     use std::__rand::thread_rng;

error[E0425]: cannot find function `random` in module `rand`
  --> rand.rs:10:27
   |
10 |         let tuple = rand::random::<(f64, char)>();
   |                           ^^^^^^ not found in `rand`

error: use of unstable library feature 'rand': use `rand` from crates.io (see issue #27703)
 --> rand.rs:1:5
  |
1 |     extern crate rand;
  |     ^^^^^^^^^^^^^^^^^^

error: use of unstable library feature 'rand': use `rand` from crates.io (see issue #27703)
 --> rand.rs:2:9
  |
2 |     use rand::Rng;
  |         ^^^^^^^^^

在很久以前, rand crate 是標准庫的一部分,但早已被提取到 crate 中 這個箱子應該是你使用的那個:

指定Cargo.toml

[package]
name = "stackoverflow"
version = "0.0.1"
authors = ["A. Developer <developer@example.com>"]

[dependencies]
rand = "0.7.0" # Or a newer version

然后您的示例代碼有效:

use rand::Rng; // 0.7.2

fn main() {
    let mut rng = rand::thread_rng();
    if rng.gen() { // random bool
        println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
    }
    let tuple = rand::random::<(f64, char)>();
    println!("{:?}", tuple)
}

隨着輸出:

$ cargo run
     Running `target/debug/so`
i32: 1819776837, u32: 3293137459
(0.6052759716514547, '\u{69a69}')

$ cargo run
     Running `target/debug/so`
(0.23882541338214436, '\u{10deee}')

為什么從 stdlib 中刪除了這些有用的函數?

Rust 的理念是將盡可能多的內容放入 crate 而不是標准庫。 這允許每段代碼以與標准庫不同的速度增長和發展,並且還允許代碼停止使用而不會強制永遠維護它。

一個常見的例子是Python 中的 HTTP 庫序列 有多個包都以不同的方式做同樣的事情,Python 維護者必須保留所有包以提供向后兼容性。

板條箱可以避免這種特殊的結果。 如果一個 crate 真的穩定了很長時間,我相信它可以重新添加到標准庫中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM