Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,406 questions

56,397 answers

573 users

How to create a list of random file names, including extension, dates, and file size in Ruby

1 Answer

0 votes
require "securerandom"

# Generate a random string of given length
def generate_random_string(length)
  charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  Array.new(length) { charset[SecureRandom.random_number(charset.length)] }.join
end

# Generate a random date
def generate_random_date
  year  = rand(2000..2020)
  month = rand(1..12)
  day   = rand(1..28)

  format("%04d-%02d-%02d", year, month, day)
end

# Generate a random file size
def generate_random_file_size
  rand(1..100_000)
end

extensions = [".txt", ".jpg", ".png", ".cpp", ".pdf"]
number_of_files = 10
file_length = 9

number_of_files.times do
  file_name = generate_random_string(file_length)
  extension = extensions.sample
  date      = generate_random_date
  file_size = generate_random_file_size

  puts "#{file_name}#{extension} #{date} #{file_size} bytes"
end



=begin
run:

T2EbtxWFW.png 2003-08-03 27665 bytes
vYPW8XBXU.jpg 2009-07-19 95754 bytes
IZ7CCX3xd.png 2012-07-22 32720 bytes
6QxrIzesR.pdf 2014-09-04 26436 bytes
w6lRCpVlD.png 2009-01-15 46181 bytes
cQW7kPPTH.txt 2014-05-21 39794 bytes
9E0NaNYBR.jpg 2006-03-01 80741 bytes
l6UfBOx1H.jpg 2008-01-28 65547 bytes
ca11Xm0ji.txt 2005-01-14 80979 bytes
hDlxlfFr5.pdf 2017-10-13 3523 bytes

=end

 



answered May 16 by avibootz

Related questions

...