Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate random Japanese characters naively
module RandomJapanese
KANJI_LOWER_RANGE = 0x4E00
KANJI_UPPER_RANGE = 0x9FBF
HIRAGANA_LOWER_RANGE = 0x3040
HIRAGANA_UPPER_RANGE = 0x309F
KATAKANA_LOWER_RANGE = 0x30A0
KATAKANA_UPPER_RANGE = 0x30FF
class << self
def kanji(length = 1)
length.times.map {
random_unicode(KANJI_LOWER_RANGE, KANJI_UPPER_RANGE)
}.join
end
def hiragana(length = 1)
length.times.map {
random_unicode(HIRAGANA_LOWER_RANGE, HIRAGANA_UPPER_RANGE)
}.join
end
def katakana(length = 1)
length.times.map {
random_unicode(KATAKANA_LOWER_RANGE, KATAKANA_UPPER_RANGE)
}.join
end
private
def random_unicode(lower, upper)
codepoint = lower + rand(upper - lower)
[codepoint].pack('U')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.