Skip to content

Instantly share code, notes, and snippets.

@dkam
Created December 28, 2023 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkam/06aca0f98cd422ee7339ed4e4719b70a to your computer and use it in GitHub Desktop.
Save dkam/06aca0f98cd422ee7339ed4e4719b70a to your computer and use it in GitHub Desktop.
Random ISBN13
def random_isbn13
isbn12 = rand(978_000_000_000..979_999_999_999).to_s
chksum = (10 - isbn12.each_char.with_index.sum { |digit, i| digit.to_i * (i.even? ? 1 : 3) } % 10 ) % 10
"#{isbn12}#{chksum}"
end
def risbn13 = rand(978_000_000_000..979_999_999_999).then {|isbn12| "#{isbn12}#{(10 - isbn12.to_s.each_char.with_index.sum { |digit, i| digit.to_i * (i.even? ? 1 : 3) } % 10 ) % 10}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment