如何让我的代码在37次不同的时间内随机读取行? def pick_random_行 选择线=零 foreach(“id'sForCascade.txt”)。每个带有索引do的| 选择的线=兰德false,“code”=>401}(hackex::HackExError)@Tobias 401听起来像是授权错误。您在哪个设备上编写此代码?你能检查一下你的Ruby进程对这个文件有文件读取权限吗?我在平板电脑上写了这篇文章,在平板电脑上进行测试很痛苦,所以我没有进行测试。如果你发现一个错误,请告诉我。 lines = (1..cnt).to_a.sample(n).sort File.foreach(FName).with_object([]) do |line,a| if lines.first == $. a << line lines.shift break a if lines.empty? end end

如何让我的代码在37次不同的时间内随机读取行? def pick_random_行 选择线=零 foreach(“id'sForCascade.txt”)。每个带有索引do的| 选择的线=兰德false,“code”=>401}(hackex::HackExError)@Tobias 401听起来像是授权错误。您在哪个设备上编写此代码?你能检查一下你的Ruby进程对这个文件有文件读取权限吗?我在平板电脑上写了这篇文章,在平板电脑上进行测试很痛苦,所以我没有进行测试。如果你发现一个错误,请告诉我。 lines = (1..cnt).to_a.sample(n).sort File.foreach(FName).with_object([]) do |line,a| if lines.first == $. a << line lines.shift break a if lines.empty? end end,ruby,Ruby,嘿,我想让代码选择37行不同的代码。那么我该怎么做呢?我被困住了,困惑了。你可以这样做: def pick_random_line chosen_line = nil File.foreach("id'sForCascade.txt").each_with_index do |line, id| chosen_line = line if rand < 1.0/(id+1) end return chosen_line end`enter code here in

嘿,我想让代码选择37行不同的代码。那么我该怎么做呢?我被困住了,困惑了。

你可以这样做:

def pick_random_line
  chosen_line = nil
  File.foreach("id'sForCascade.txt").each_with_index do |line, id|
    chosen_line = line if rand < 1.0/(id+1)
  end
  return chosen_line
end`enter code here
input_lines = File.foreach("test.txt").map(&:to_s)
output_lines = []
37.times do
  output_lines << input_lines.delete_at(rand(input_lines.length))
end

puts output_lines
input_lines=File.foreach(“test.txt”).map(&:to_s)
输出_行=[]
37.5倍

输出行假设您不希望同一行重复多次,我将在一行中这样做:

def pick_random_line
  chosen_line = nil
  File.foreach("id'sForCascade.txt").each_with_index do |line, id|
    chosen_line = line if rand < 1.0/(id+1)
  end
  return chosen_line
end`enter code here
input_lines = File.foreach("test.txt").map(&:to_s)
output_lines = []
37.times do
  output_lines << input_lines.delete_at(rand(input_lines.length))
end

puts output_lines
File.read(“test.txt”)
读取整个文件

split(“\n”)
根据
\n
分隔符将文件拆分为多行(我假设您的文件是文本文件,并用新行字符分隔行)

shuffle
是一种非常方便的
Array
方法,它随机洗牌行。您可以在此处阅读:


最后,
first(37)
给出了无序数组中的前37行。通过shuffle
操作,这些保证是随机的。

如果您想从一个大文件中保存相对较少的行,那么将整个文件读入一个数组(然后随机选择行)可能代价高昂。最好是计算文件中的行数,随机选择行偏移,然后将这些偏移处的行保存到数组中。与前一种方法相比,这种方法实现起来并不困难,但它使该方法更加健壮,即使当前应用程序中的文件不太大。1

假设您的文件名由
FName
给出。以下是计算文件中行数的三种方法:

逐字数数行

使用

在Unix系列计算机上,使用操作系统

第三种选择非常快

要保存的
n
行的随机偏移量(基本
1
)可计算如下:

 cnt = %x{wc -l #{FName}}.split.first.to_ii
将这些偏移处的线保存到阵列非常简单;例如:

lines = (1..cnt).to_a.sample(n).sort
File.foreach(FName)|
如果lines.first==$。
读取第一行后的1
,读取第一行后的
$。
在读取每一连续行后递增
1
。(因此线偏移的基准
1


1此外,许多程序员,不仅仅是鲁比斯特,都对积累大量的东西然后抛弃除少数以外的所有东西的想法感到厌恶

谢谢!但是由于某种原因,我的脚本给了我一个错误,因为我试图调用新的line@Tobias奇怪的你能试着运行
File.foreach(“test.txt”)。来代替shuffle.first(37)
吗?我不知道让我试试吧:/Users/EliLap/Documents/MyLib/lib/hackex/net/std.rb:38:'Do':不成功:{“success”=>false,“code”=>401}(hackex::HackExError)@Tobias 401听起来像是授权错误。您在哪个设备上编写此代码?你能检查一下你的Ruby进程对这个文件有文件读取权限吗?我在平板电脑上写了这篇文章,在平板电脑上进行测试很痛苦,所以我没有进行测试。如果你发现一个错误,请告诉我。
lines = (1..cnt).to_a.sample(n).sort
File.foreach(FName).with_object([]) do |line,a|
  if lines.first == $.
    a << line
    lines.shift
    break a if lines.empty?
  end
end