mercredi 25 mai 2016

Generate a random string in the fastest way

I am currently using this:

(defvar my-charset
  (eval-when-compile
    (concat (number-sequence 48 57) (number-sequence 65 90) (number-sequence 97 122)))
  "Char set in terms of number list.")
(defvar my-charset-length
  (eval-when-compile
    (length (concat (number-sequence 48 57) (number-sequence 65 90) (number-sequence 97 122))))
  "Length of my-charset.")

(defun my-generate-string (&optional max-length min-length)
  "Generate a random string."
  (let (string)
    (dotimes (_i (+ (random (- (or max-length 10) (or min-length 5) -1)) (or min-length 5)))
      (push (aref my-charset (random my-charset-length)) string))
    (concat string)))

Any method to make it faster?

Or any other way to generate the string much faster?




Aucun commentaire:

Enregistrer un commentaire