Skip to content

Instantly share code, notes, and snippets.

@l15n
Created August 26, 2014 01:31
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 l15n/d3d8974228134bc0b1a1 to your computer and use it in GitHub Desktop.
Save l15n/d3d8974228134bc0b1a1 to your computer and use it in GitHub Desktop.
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