help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Generate random char (and string) from unicode category (e.g: letter)


From: Alexandre Garreau
Subject: Generate random char (and string) from unicode category (e.g: letter)
Date: Thu, 06 Dec 2018 11:44:38 +0100
User-agent: Gnus (5.13), GNU Emacs 25.1.1 (i686-pc-linux-gnu, GTK+ Version 3.22.11) of 2017-09-15, modified by Debian

Hi,

I recall clearly having wrote in elisp something to generate random and
more-or-less plausible input for gmail account creation form, including
ascii chars for login, statistical randomness for gender (like, iirc,
48% of “male”, 52% of “female”, minus 2% of “others”), and random
unicode for password, real name, etc. I recall in the end I ended with a
lot of ideograms in those.  So I know it’s doable in pure elisp (or
maybe was it guile? less likely…).

I really don’t recall how I did that, nor if I took care of using a
single script for each form input, but I’m sure I was using something
less ugly than currently, that is, (random (max-char)) until it matches
[[:alpha:]] (but I clearly recall using something that would work for
all unicode, including foreign scripts I wouldn’t even know about).

Do you have an idea of something cleaner? currently I have this:

#+BEGIN_SRC emacs-lisp
(defun random-letter (&rest osef)
  (let ((num (random (max-char))))
    (until (string-match "[[:alpha:]]" (string num))
      (setq num (random (max-char))))
    num))
#+END_SRC

and use it like this:

#+BEGIN_SRC emacs-lisp
(apply #'string (mapcar #'random-letter (make-list (1+ (random 190)) nil)))
#+END_SRC

Problems is I get stuff like this: "䯩繩ꏴ跾ಾ𢉰𐎕𘓅𪆶矏ᄬ𣒈⳰𨜄𛰸𧅌煂𢙴𧐁𡚯
𦅉ᤋ𡇼钇꿚㱓㗧𩅍姵爠𣑽𠌤ꇊ𡘄𑄇𫲘𪯋𣊚𦉂𠦵𘕋𠈾ლ𨟇𦷕𤃻𫿡𢿟巙𩿊𥖠𒒈ባ𗆘𤧟𗲀𔗸𖼂뺔
𧸋𡠜𬶟咨발𬞗쏊紋䲁坮𠢥旼𗴟𬓏𤁍គ𩍏Ɉ𪅊𤙬𫪃𫴛𤶋𫴃𧐨䞪𩇨𡤦馲𨂧𡮃𓂅𒇵𤉴𥙯藣ბ솇
𨆬𦄎靔𐤒ඐ𒐕襋𬵝𥤄𪃝𫈹𨣼𘋹돃𪣞筛𣯿휈𥽊Ꭾ𣐧𥺒𠊆ꮩ闭ઉ𦻸𨔆𤛢𢮁𤟩𪊕𥫰𪢟𡻋𘅇ᶗ펙𣄽
玽쭻𩿎𗔥𪟉䪁ᣃ쒝𩅑𡞬넒煮ڒꫥ𥾴𣁫𬑼깙𣫖筁ᣯ𣱮𡡯𨐒ﶒ𤑳𤯼昵䊘㝓𣑼𐦥𥆤갛𤡇𠜠𥉡䋯𥫻
𪲩兀𖠹瀖𣊫𨘥𢍪ᴢ", where the majority of characters are non-displayable
and are shown with a square with numbers in it to indicate there’s
nothing such that in installed fonts.  I clearly recall I what I did
there were no such characters, so something must be possible (maybe
using charsets?).

PS: is there a way to get something else than linear random distribution
with `random'? like normal law, or logarithmic distribution?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]