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

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

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


From: Ben Bacarisse
Subject: Re: Generate random char (and string) from unicode category (e.g: letter)
Date: Thu, 06 Dec 2018 13:46:27 +0000

Alexandre Garreau <galex-713@galex-713.eu> writes:

> 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: "䯩繩ꏴ跾ಾ𢉰𐎕𘓅𪆶矏ᄬ𣒈⳰𨜄𛰸𧅌煂𢙴𧐁

You might find char-displayable-p useful.  It returns 'unicode for those
numbered character for which there is no configured font.  It returns t
for others but that includes control characters.

describe-char-display gives the font being used and will exclude control
characters.  It needs two args -- a position and a char or number -- but
the position is ignored when there is an actual character.

Unlike char-displayable-p is it not documented so it may change or
vanish over time.

char-syntax returns ?w for word-like characters.  This might do instead
on the [[:alpha:]] match.  Thus

(let ((ch (random (max-char))))
  (and (char-displayable-p ch) (eq (char-syntax ch) ?w)))

might be what you want though there will be a relatively low density of
matching characters.  max-char is very big.

Another strategy is to select characters randomly from a string of
acceptable options.

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

Yes, but I am running out of time!  A cheap way to get an almost normal
distribution with mean n is to sum k numbers between 0 and n/k.

If you use the "select from a string" method, you can simply duplicate
those characters you want more of.

-- 
Ben.


reply via email to

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