将 Ruby 运行为 index.cgi, [1,3,5].shuffle 总是会产生相同的结果
我确实转储了 RUBY_VERSION => 的值1.8.7
每次[1,3,5].shuffle的值也是[1,3,5] 我必须在它前面添加一个 srand(Time.now.to_i) 或 srand() 以使其随机...... 我以为 srand 会自动调用?但也许不在 .cgi 环境中?
如果我使用 irb,查看 [1,3,5].shuffle,然后退出,然后重新输入 irb,每次结果都不同。
顺便说一句, ri shuffle 没有给出任何内容,并且 Array 和 Enumerable 文档没有列出 shuffle 或 shuffle!任何一个... ?
I do dump the value of RUBY_VERSION => 1.8.7
every time, the value of [1,3,5].shuffle is also [1,3,5]
i have to add a srand(Time.now.to_i) or srand() in front of it to make it random...
I thought srand is automatically called? but maybe not in a .cgi environment?
if i use irb, and look at [1,3,5].shuffle, and exit, and re-enter irb, each time the results are different.
by the way, ri shuffle
didn't give anything, and the Array and Enumerable docs didn't list shuffle or shuffle! either... ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我看不到您如何检查它是否已更改,因此我不能肯定地说,但我希望问题必须处理您如何检查它是否已更改。如果使用随机播放,则不会改变原始数组。因此,如果您检查原始数组的值而不是返回的结果,则该方法每次都会返回相同的值
此外,这里是文档 http://ruby-doc.org/core-1.8.7/classes/Array.html#M000335
Since I can't see how you checked to see whether it had changed or not, I can't say for sure, but I expect the issue has to deal with how you are checking whether it has changed or not. If you use shuffle, that does not alter the original array. So if you check the value of the original array rather than the returned result, it will appear that the method is returning the same value each time
Also, here are the docs http://ruby-doc.org/core-1.8.7/classes/Array.html#M000335