Skip to main content
edited body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Generate random 10% of file to be used in testing with rubyRuby

I'm new to rubyRuby, but not to programming. I just need a simple script that, given a text file, will pull out around 10% of the lines at random. Below is what I came up with based upon a pythonPython script I wrote. What do I need to do differently to make it more rubyRuby-like?

prng = Random.new

File.open('english-words-partial.txt', 'w') {  |f| 
  File.readlines('english-words-full.txt').each do |line| 
    if prng.rand >= 0.9
      f.write(line) 
    end
  end
}

Generate random 10% of file to be used in testing with ruby

I'm new to ruby, but not to programming. I just need a simple script that given a text file will pull out around 10% of the lines at random. Below is what I came up with based upon a python script I wrote. What do I need to do differently to make it more ruby-like?

prng = Random.new

File.open('english-words-partial.txt', 'w') {  |f| 
  File.readlines('english-words-full.txt').each do |line| 
    if prng.rand >= 0.9
      f.write(line) 
    end
  end
}

Generate random 10% of file to be used in testing with Ruby

I'm new to Ruby, but not to programming. I just need a simple script that, given a text file, will pull out around 10% of the lines at random. Below is what I came up with based upon a Python script I wrote. What do I need to do differently to make it more Ruby-like?

prng = Random.new

File.open('english-words-partial.txt', 'w') {  |f| 
  File.readlines('english-words-full.txt').each do |line| 
    if prng.rand >= 0.9
      f.write(line) 
    end
  end
}
edited tags
Link
pgreen2
  • 639
  • 3
  • 13
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
remove final answer to follow rules
Source Link
pgreen2
  • 639
  • 3
  • 13
Loading
added refactored code
Source Link
pgreen2
  • 639
  • 3
  • 13
Loading
added refactored code
Source Link
pgreen2
  • 639
  • 3
  • 13
Loading
Source Link
pgreen2
  • 639
  • 3
  • 13
Loading