如何在Rust 1.0中获得随机数? [英] How can I get a random number in Rust 1.0?

查看:316
本文介绍了如何在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板条箱是标准库的一部分,但很久以来一直是

然后您的示例代码起作用:

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的理念是将尽可能多的箱子放置在板条箱中,而不是放在标准库中.这样一来,每段代码都可以以与标准库不同的速率增长和发展,并且还可以停止使用而无需强制对其进行永久维护.

一个常见的示例是序列Python中的HTTP库.有多个软件包都以不同的方式执行相同的操作,并且Python维护人员必须保留全部 以便提供向后兼容性.

板条箱可以避免这种特殊的结果.如果一个板条箱可以长期稳定,我敢肯定可以将其重新添加到标准库中.

I tried

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);
}

but this gives

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};
  |                           ^^^

and I tried

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)
}

and got

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

解决方案

In the far past, the rand crate was part of the standard library but has long since been extracted to a crate. This crate should be the one you use:

Specify a Cargo.toml:

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

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

Then your example code works:

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)
}

With the output:

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

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

Why were these useful functions removed from stdlib?

Rust has a philosophy of placing as much as possible into crates instead of the standard library. This allows each piece of code to grow and evolve at a different rate than the standard library and also allows the code to stop being used without forcing it to be maintained forever.

A common example is the sequence of HTTP libraries in Python. There are multiple packages that all do the same thing in different ways and the Python maintainers have to keep all of them to provide backwards compatibility.

Crates allow this particular outcome to be avoided. If a crate truly stabilizes for a long time, I'm sure it could be re-added to the standard library.

这篇关于如何在Rust 1.0中获得随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆