Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Generating alphanumeric gibberish?

Name: Anonymous 2010-01-12 14:58

What's a simple (preferably loopless) way to generate a large amount of alphanumeric gibberish? Any language is acceptable.

In PHP, I thought this would be perfect:

hexdec(mt_rand(1.e999999999, 1.e9999999999));

But it doesn't work due to restrictions on the length of int. :(

Name: Anonymous 2010-01-12 15:14


(declaim (inline pick-random))
(defun pick-random (sequence &aux (length (length sequence)))
  (elt sequence (random length)))

(defconstant +valid-chars+ "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjklmopqrst")

(defun generate-random-text (length &optional (valid-chars +valid-chars+))
  (concatenate 'string
       (loop repeat length collect (pick-random valid-chars))))

;CL-USER> (generate-random-text 100)
;"r6PAJAdYEMHd6mbha6bkaYLqAFTR2CoCM455IYP5RbKpH5XWNBXJJ4IVTBtgG9AODa6X8FAWCb5oOP4qPhQCjIt1lq1G76sQsZKD"

;Benchmark:
;CL-USER> (time (loop repeat 100000 do (generate-random-text 100)))
;Evaluation took:
;  1.375 seconds of real time
;  1.375000 seconds of total run time (1.375000 user, 0.000000 system)
;  [ Run times consist of 0.080 seconds GC time, and 1.295 seconds non-GC time. ]
;  100.00% CPU
;  3,299,809,761 processor cycles
;  136,798,984 bytes consed
 
;;; Here's a slightly more efficient implementation:
(defun generate-random-text (length &optional (valid-chars +valid-chars+))
  (let ((string (make-string length)))
    (dotimes (i length)
      (setf (aref string i) (pick-random valid-chars)))))

;CL-USER> (time (loop repeat 100000 do (generate-random-text 100)))
;Evaluation took:
;  0.703 seconds of real time
;  0.703125 seconds of total run time (0.687500 user, 0.015625 system)
;  100.00% CPU
;  1,684,737,900 processor cycles
;  41,602,096 bytes consed

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List