all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
To: help-gnu-emacs@gnu.org
Subject: Re: Generate random char (and string) from unicode category (e.g: letter)
Date: Thu, 06 Dec 2018 13:46:27 +0000	[thread overview]
Message-ID: <87r2eu3gzw.fsf@bsb.me.uk> (raw)
In-Reply-To: mailman.5272.1544093503.1284.help-gnu-emacs@gnu.org

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	other threads:[~2018-12-06 13:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.5272.1544093503.1284.help-gnu-emacs@gnu.org>
2018-12-06 13:46 ` Ben Bacarisse [this message]
2018-12-06 14:29   ` Generate random char (and string) from unicode category (e.g: letter) Emanuel Berg
2018-12-06 10:44 Alexandre Garreau
2018-12-06 11:24 ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r2eu3gzw.fsf@bsb.me.uk \
    --to=ben.usenet@bsb.me.uk \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.