如何在Rust中某个范围内生成随机数? [英] How can I generate a random number within a range in Rust?

查看:1771
本文介绍了如何在Rust中某个范围内生成随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编者注:此代码示例来自1.0之前的Rust版本,在语法上不是有效的Rust 1.0代码.这段代码的更新版本会产生不同的错误,但是答案仍然包含有价值的信息.

Editor's note: This code example is from a version of Rust prior to 1.0 and is not syntactically valid Rust 1.0 code. Updated versions of this code produce different errors, but the answers still contain valuable information.

我遇到了以下示例,该示例说明了如何使用Rust生成随机数,但是它似乎不起作用.该示例未显示其适用于Rust的哪个版本,因此它可能已过时,或者我弄错了.

I came across the following example of how to generate a random number using Rust, but it doesn't appear to work. The example doesn't show which version of Rust it applies to, so perhaps it is out-of-date, or perhaps I got something wrong.

// http://static.rust-lang.org/doc/master/std/rand/trait.Rng.html

use std::rand;
use std::rand::Rng;

fn main() {
    let mut rng = rand::task_rng();
    let n: uint = rng.gen_range(0u, 10);
    println!("{}", n);
    let m: float = rng.gen_range(-40.0, 1.3e5);
    println!("{}", m);
}

当我尝试对此进行编译时,会出现以下错误:

When I attempt to compile this, the following error results:

test_rand002.rs:6:17: 6:39 error: type `@mut std::rand::IsaacRng` does not
implement any method in scope named `gen_range`
test_rand002.rs:6    let n: uint = rng.gen_range(0u, 10);
                                   ^~~~~~~~~~~~~~~~~~~~~~
test_rand002.rs:8:18: 8:46 error: type `@mut std::rand::IsaacRng` does not
implement any method in scope named `gen_range`
test_rand002.rs:8    let m: float = rng.gen_range(-40.0, 1.3e5);
                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

在同一页面(上面)上还有另一个有效的示例(如下).但是,尽管我可以适应它,但它并不能完全满足我的要求.

There is another example (as follows) on the same page (above) that does work. However, it doesn't do exactly what I want, although I could adapt it.

use std::rand;
use std::rand::Rng;

fn main() {
    let mut rng = rand::task_rng();
    let x: uint = rng.gen();
    println!("{}", x);
    println!("{:?}", rng.gen::<(f64, bool)>());
}

如何在指定范围(例如:0到n)之间使用Rust(例如:i64)生成一个简单"随机数?

How can I generate a "simple" random number using Rust (e.g.: i64) within a given range (e.g.: 0 to n)?

推荐答案

编者注:该答案适用于1.0之前的Rust版本,并且在Rust 1.0中无效.

Editor's note: This answer is for a version of Rust prior to 1.0 and is not valid in Rust 1.0.

最近这已经发生了很大变化(对不起!都是我),在Rust 0.8中,它被称为

This has been changing a lot recently (sorry! it's all been me), and in Rust 0.8 it was called gen_integer_range (note the /0.8/ rather than /master/ in the URL, if you are using 0.8 you need to be reading those docs).

警告:.gen_integer_range在许多方面都是完全不正确的,新的.gen_range没有不正确性问题.

A word of warning: .gen_integer_range was entirely incorrect in many ways, the new .gen_range doesn't have incorrectness problems.

主代码(在.gen_range正常工作的地方):

Code for master (where .gen_range works fine):

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

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

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