Created
December 28, 2023 04:07
-
-
Save dkam/06aca0f98cd422ee7339ed4e4719b70a to your computer and use it in GitHub Desktop.
Random ISBN13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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