ruby 生成随机字符串_如何在Ruby中生成随机数

ruby 生成随机字符串

While no computer can generate truly random numbers, Ruby does provide access to a method that will return pseudorandom numbers.

尽管没有计算机能够生成真正的随机数,但是Ruby确实提供了对将返回伪随机数的方法的访问。

数字实际上不是随机的 ( The Numbers Aren't Actually Random )

No computer can generate truly random numbers purely by computation. The best they can do is to generate pseudorandom numbers, which are a sequence of numbers that appear random but are not.

没有计算机能够完全通过计算来生成真正的随机数 。 他们所能做的最好的事情就是生成伪随机数,它是一个数字序列, 看起来随机但不是随机的。

To a human observer, these numbers are indeed random. There will be no short repeating sequences, and, at least to the human observer, they'll present no clear pattern. However, given enough time and motivation, the original seed can be discovered, the sequence recreated and the next number in the sequence guessed.

对于人类观察者来说,这些数字确实是随机的。 不会有短的重复序列,并且至少对于人类观察者而言,它们不会呈现清晰的图案。 但是,只要有足够的时间和动力,就可以发现原始种子 ,重新创建序列,并猜测序列中的下一个数字。

For this reason, the methods discussed in this article should probably not be used to generate numbers that must be cryptographically secure.

出于这个原因,本文中讨论的方法可能不应用于生成必须加密安全的数字。

Pseudorandom number generators must be seeded in order to produce sequences that differ each time a new random number is generated. No method is magical — these seemingly random numbers are generated using relatively simple algorithms and relatively simple arithmetic. By seeding the PRNG, you're starting it off at a different point every time. If you didn't seed it, it would generate the same sequence of numbers each time.

必须植入伪随机数生成器,以产生每次生成新随机数时都不同的序列。 没有一种方法是不可思议的-这些看似随机的数字是使用相对简单的算法和相对简单的算法生成的。 通过植入PRNG,您每次都在不同的位置启动它。 如果不进行播种,则每次都会生成相同的数字序列。

In Ruby, the Kernel#srand method can be called with no arguments. It will choose a random number seed based on the time, the process ID and a sequence number. Simply by calling srand anywhere at the beginning of your program, it will generate a different series of seemingly random numbers each time you run it. This method is called implicitly when the program starts up, and seeds the PRNG with the time and process ID (no sequence number).

在Ruby中,可以不带任何参数调用Kernel#srand方法。 它将基于时间,进程ID和序列号选择一个随机数种子。 只需在程序开始时在任何地方调用srand ,每次您运行它时,它就会生成一系列不同的看似随机数。 程序启动时隐式调用此方法,并为PRNG注入时间和进程ID(无序列号)。

产生数字 ( Generating Numbers )

Once the program is running and Kernel#srand was either implicitly or explicitly called, the Kernel#rand method can be called. This method, called with no arguments, will return a random number from 0 to 1. In the past, this number was typically scaled to the maximum number you'd wish to generate and perhaps to_i called on it to convert it to an integer.

一旦程序运行并且隐式或显式调用了Kernel# srand,就可以调用Kernel#rand方法。 此方法(不带任何参数)将返回一个从0到1的随机数。在过去,该数字通常按比例缩放为您希望生成的最大数字,并且可能调用了to_i将其转换为整数。

# Generate an integer from 0 to 10
puts (rand() * 10).to_i

However, Ruby makes things a bit easier if you're using Ruby 1.9.x. The Kernel#rand method can take a single argument. If this argument is a Numeric of any kind, Ruby will generate an integer from 0 up to (and not including) that number.

但是,如果您使用Ruby 1.9.x,则Ruby会使事情变得容易一些。 Kernel#rand方法可以采用单个参数。 如果此参数是任何数字 ,Ruby将生成一个从0到(不包括)该数字的整数。

# Generate a number from 0 to 10
# In a more readable way
puts rand(10)

However, what if you want to generate a number from 10 to 15? Typically, you'd generate a number from 0 to 5 and add it to 10. However, Ruby makes it easier.

但是,如果要生成10到15之间的数字怎么办? 通常,您会生成一个介于0到5之间的数字,并将其添加到10。但是,Ruby使它变得更容易。

You can pass a Range object to Kernel#rand and it will do just as you'd expect: generate a random integer in that range.

您可以将Range对象传递给Kernel#rand ,它将如您所愿地进行:在该范围内生成一个随机整数。

Make sure you pay attention to the two types of ranges. If you called rand(10..15), that would generate a number from 10 to 15 including 15. Whereas rand(10...15) (with 3 dots) would generate a number from 10 to 15 not including 15.

确保注意两种类型的范围。 如果调用rand(10..15) ,则将生成10到15之间的数字, 包括 15。而rand(10 ... 15) (带有3个点)将生成从10到15之间的数字( 不包括 15)。

# Generate a number from 10 to 15
# Including 15
puts rand(10..15)

非随机数 ( Non-Random Random Numbers )

Sometimes you need a random-looking sequence of numbers, but need to generate the same sequence every time. For example, if you generate random numbers in a unit test, you should generate the same sequence of numbers every time.

有时您需要一个看起来随机的数字序列,但是每次都需要生成相同的序列。 例如,如果您在单元测试中生成随机数,则每次都应生成相同的数字序列。

A unit test that fails on one sequence should fail again the next time it's run, if it generated a difference sequence the next time, it might not fail. To do that, call Kernel#srand with a known and constant value.

在一个序列上失败的单元测试应该在下次运行时再次失败,如果下次生成差异序列,它可能不会失败。 为此,请使用已知且恒定的值调用Kernel#srand

# Generate the same sequence of numbers every time
# the program is run srand(5)
# Generate 10 random numbers
puts (0..10).map{rand(0..10)}

有一个警告 ( There is One Caveat )

The implementation of Kernel#rand is rather un-Ruby. It doesn't abstract the PRNG in any way, nor does it allow you to instantiate the PRNG. There is one global state for the PRNG that all the code shares. If you change the seed or otherwise change the state of the PRNG, it may have a wider range of effect than you anticipated.

Kernel#rand的实现是非Ruby的。 它不以任何方式抽象PRNG,也不允许您实例化PRNG。 所有代码共享的PRNG处于一种全局状态。 如果更改种子或以其他方式更改PRNG的状态,则其作用范围可能超出您的预期。

However, since programs expect the result of this method to be random — that's its purpose! — this will probably never be a problem. Only if the program expects to see an expected sequence of numbers, such as if it had called srand with a constant value, should it see unexpected results.

但是,由于程序期望此方法的结果是随机的-这就是它的目的! -这可能永远不会成为问题。 仅当程序期望看到期望的数字序列时(例如,它已经调用了具有恒定值的srand) ,才可以看到意外的结果。

翻译自: https://www.thoughtco.com/generating-random-numbers-in-ruby-2908088

ruby 生成随机字符串

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值