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

Pages: 1-

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:05

why no loops

Name: Anonymous 2010-01-12 15:09

oh wow

Name: Anonymous 2010-01-12 15:12

>>2
I want it on one line.

Also:

rtrim(base64_encode(mcrypt_create_iv(13000, MCRYPT_DEV_URANDOM)), '=');

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

Name: Anonymous 2010-01-12 15:16

Oops, minor mistake there:

(defun generate-random-text (length &optional (valid-chars +valid-chars+))
  (let ((string (make-string length)))
    (dotimes (i length string)
      (setf (aref string i) (pick-random valid-chars)))))

Name: Anonymous 2010-01-12 15:18

>>5
Lol, Lisp takes an entire second to randomly pick 100 characters?

Name: Anonymous 2010-01-12 15:20

>>7
Learn to read. It takes 0.7seconds to generate 100 characters 100000 times, and I didn't even crank up optimizationsettings.

Name: >>8 2010-01-12 15:25

>>7
I didn't benchmark a single call as it would have been hard to tell how much it took (it's just too fast, and inconclusive in a multi-tasked environment). But if you want it that much, here it is:


CL-USER> (time (generate-random-text 100))
Evaluation took:
  0.000 seconds of real time
  0.000000 seconds of total run time (0.000000 user, 0.000000 system)
  100.00% CPU
  18,873 processor cycles
  0 bytes consed
 
"GV6RTC4m3qSglNJdDeo2HNoLcf07TCLTooCTSIOcGfF7eM4RL7FdXVcUeQd4q3m5emZ4H0LLU3rVL1749GTarqKY0ZGsVtId0DKm"

Oh, and I don't see PHP generating native code either:

CL-USER> (disassemble #'generate-random-text)
; disassembly for GENERATE-RANDOM-TEXT
; 24ABA4E0:       .ENTRY GENERATE-RANDOM-TEXT(LENGTH &OPTIONAL (VALID-CHARS +VALID-CHARS+))  ; (FUNCTION
                                                                                             ;  (T ..))
;      4FC:       8F4504           POP DWORD PTR [EBP+4]
;      4FF:       8D65E8           LEA ESP, [EBP-24]
;      502:       83F904           CMP ECX, 4
;      505:       7411             JEQ L1
;      507:       83F908           CMP ECX, 8
;      50A:       7507             JNE L0
;      50C:       8BC2             MOV EAX, EDX
;      50E:       897DF8           MOV [EBP-8], EDI
;      511:       EB10             JMP L2
;      513: L0:   CC0A             BREAK 10                   ; error trap
;      515:       02               BYTE #X02
;      516:       18               BYTE #X18                  ; INVALID-ARG-COUNT-ERROR
;      517:       4D               BYTE #X4D                  ; ECX
;      518: L1:   8BC2             MOV EAX, EDX
;      51A:       8B0DD0A4AB24     MOV ECX, [#x24ABA4D0]      ; "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjklmopqrst"
;      520:       894DF8           MOV [EBP-8], ECX
;      523: L2:   8945F0           MOV [EBP-16], EAX
;      526:       8D4804           LEA ECX, [EAX+4]
;      529:       8D510F           LEA EDX, [ECX+15]
;      52C:       83E2F8           AND EDX, -8
;      52F:       892D2C041022     MOV [#x2210042C], EBP
;      535:       031578D14100     ADD EDX, [#x41D178]        ; _boxed_region
;      53B:       3B157CD14100     CMP EDX, [#x41D17C]
;      541:       7607             JBE L3
;      543:       E8387E95DB       CALL #x412380              ; _alloc_overflow_edx
;      548:       EB12             JMP L4
;      54A: L3:   331578D14100     XOR EDX, [#x41D178]        ; _boxed_region
;      550:       311578D14100     XOR [#x41D178], EDX        ; _boxed_region
;      556:       331578D14100     XOR EDX, [#x41D178]        ; _boxed_region
;      55C: L4:   8D5207           LEA EDX, [EDX+7]
;      55F:       C642F9AA         MOV BYTE PTR [EDX-7], 170
;      563:       8942FD           MOV [EDX-3], EAX
;      566:       312D2C041022     XOR [#x2210042C], EBP
;      56C:       7402             JEQ L5
;      56E:       CC09             BREAK 9                    ; pending interrupt trap
;      570: L5:   8955F4           MOV [EBP-12], EDX
;      573:       C745FC00000000   MOV DWORD PTR [EBP-4], 0
;      57A:       EB66             JMP L7
;      57C: L6:   8B55F8           MOV EDX, [EBP-8]
;      57F:       83EC0C           SUB ESP, 12
;      582:       896C2404         MOV [ESP+4], EBP
;      586:       8D6C2404         LEA EBP, [ESP+4]
;      58A:       B904000000       MOV ECX, 4
;      58F:       FF15E4051022     CALL DWORD PTR [#x221005E4]
;      595:       8D5C24F8         LEA EBX, [ESP-8]
;      599:       83EC0C           SUB ESP, 12
;      59C:       8B05D4A4AB24     MOV EAX, [#x24ABA4D4]      ; #<FDEFINITION object for RANDOM>
;      5A2:       B904000000       MOV ECX, 4
;      5A7:       892B             MOV [EBX], EBP
;      5A9:       8BEB             MOV EBP, EBX
;      5AB:       FF5005           CALL DWORD PTR [EAX+5]
;      5AE:       8BFA             MOV EDI, EDX
;      5B0:       8D5C24F8         LEA EBX, [ESP-8]
;      5B4:       83EC0C           SUB ESP, 12
;      5B7:       8B55F8           MOV EDX, [EBP-8]
;      5BA:       8B05D8A4AB24     MOV EAX, [#x24ABA4D8]      ; #<FDEFINITION object for ELT>
;      5C0:       B908000000       MOV ECX, 8
;      5C5:       892B             MOV [EBX], EBP
;      5C7:       8BEB             MOV EBP, EBX
;      5C9:       FF5005           CALL DWORD PTR [EAX+5]
;      5CC:       C1EA08           SHR EDX, 8
;      5CF:       8B45F4           MOV EAX, [EBP-12]
;      5D2:       8B4DFC           MOV ECX, [EBP-4]
;      5D5:       89540801         MOV [EAX+ECX+1], EDX
;      5D9:       8B45FC           MOV EAX, [EBP-4]
;      5DC:       83C004           ADD EAX, 4
;      5DF:       8945FC           MOV [EBP-4], EAX
;      5E2: L7:   8B45FC           MOV EAX, [EBP-4]
;      5E5:       3B45F0           CMP EAX, [EBP-16]
;      5E8:       7C92             JL L6
;      5EA:       8B55F4           MOV EDX, [EBP-12]
;      5ED:       8BE5             MOV ESP, EBP
;      5EF:       F8               CLC
;      5F0:       5D               POP EBP
;      5F1:       C3               RET

Name: Anonymous 2010-01-12 15:34

public String gibberjabber(String in, int x, Random rand) { return ( x <= 0 ? in : gibberjabber(in+"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".charAt(rand.nextInt()%62), x-1, rand) ); }

I think.  You'll have to import java.util.Random.
To use it: this.gibberjabber("", x, new Random());

Name: Anonymous 2010-01-12 15:38

sub babble($){pack "C*",map{0x20+rand*0x40}1..shift}

Name: >>8 2010-01-12 15:46

Hmm, my code isn't as efficient as it could be according to the disassembly, that ELT doesn't get transformed into an AREF by the compiler as it doesn't know the type. Here's a quickly hacked up version that speeds it up some 4 times:

;;; Even more efficient implementation:
(defconstant +valid-chars+ "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjklmopqrst")

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

(defun generate-random-alphanumerics (length)
  (let ((string (make-string length))
        (a-len (length +valid-chars+)))
    (dotimes (i length string)
      (setf (aref string i) (aref +valid-chars+ (random a-len))))))

CL-USER> (time (loop repeat 100000 do (generate-random-alphanumerics 100)))
Evaluation took:
  0.172 seconds of real time
  0.171875 seconds of total run time (0.171875 user, 0.000000 system)
  [ Run times consist of 0.015 seconds GC time, and 0.157 seconds non-GC time. ]
  100.00% CPU
  436,756,122 processor cycles
  41,596,880 bytes consed
CL-USER> (time (generate-random-alphanumerics 100))
Evaluation took:
  0.000 seconds of real time
  0.000000 seconds of total run time (0.000000 user, 0.000000 system)
  100.00% CPU
  15,948 processor cycles
  0 bytes consed
 
"4tZXRLNVKpC5oPQgQpSsDpX87aYZs88GOR1bSamksJ7X5tU5SPA717sKEjHlXYe8KGQAL28ZPYfVlO6h0JlrDH9JIdMtPS7Lp2CQ"
CL-USER> (disassemble #'generate-random-alphanumerics)
; disassembly for GENERATE-RANDOM-ALPHANUMERICS
; 23F16042:       8BFA             MOV EDI, EDX               ; no-arg-parsing entry point
;      044:       8D4204           LEA EAX, [EDX+4]
;      047:       8D700F           LEA ESI, [EAX+15]
;      04A:       83E6F8           AND ESI, -8
;      04D:       892D2C041022     MOV [#x2210042C], EBP
;      053:       033578D14100     ADD ESI, [#x41D178]        ; _boxed_region
;      059:       3B357CD14100     CMP ESI, [#x41D17C]
;      05F:       7607             JBE L0
;      061:       E85AC34FDC       CALL #x4123C0              ; _alloc_overflow_esi
;      066:       EB12             JMP L1
;      068: L0:   333578D14100     XOR ESI, [#x41D178]        ; _boxed_region
;      06E:       313578D14100     XOR [#x41D178], ESI        ; _boxed_region
;      074:       333578D14100     XOR ESI, [#x41D178]        ; _boxed_region
;      07A: L1:   8D7607           LEA ESI, [ESI+7]
;      07D:       C646F9AA         MOV BYTE PTR [ESI-7], 170
;      081:       8956FD           MOV [ESI-3], EDX
;      084:       312D2C041022     XOR [#x2210042C], EBP
;      08A:       7402             JEQ L2
;      08C:       CC09             BREAK 9                    ; pending interrupt trap
;      08E: L2:   C745F800000000   MOV DWORD PTR [EBP-8], 0
;      095:       EB77             JMP L5
;      097: L3:   8B051860F123     MOV EAX, [#x23F16018]      ; '*RANDOM-STATE*
;      09D:       8B40FD           MOV EAX, [EAX-3]
;      0A0:       8B4007           MOV EAX, [EAX+7]
;      0A3:       8945FC           MOV [EBP-4], EAX
;      0A6:       8B5DFC           MOV EBX, [EBP-4]
;      0A9:       8B4B09           MOV ECX, [EBX+9]
;      0AC:       81F970020000     CMP ECX, 624
;      0B2:       7509             JNE L4
;      0B4:       8BC3             MOV EAX, EBX
;      0B6:       E8DCA30EFE       CALL #x22000497            ; RANDOM-MT19937-UPDATE
;      0BB:       31C9             XOR ECX, ECX
;      0BD: L4:   8B548B0D         MOV EDX, [EBX+ECX*4+13]
;      0C1:       C1EA0B           SHR EDX, 11
;      0C4:       33548B0D         XOR EDX, [EBX+ECX*4+13]
;      0C8:       8BC2             MOV EAX, EDX
;      0CA:       41               INC ECX
;      0CB:       C1E007           SHL EAX, 7
;      0CE:       894B09           MOV [EBX+9], ECX
;      0D1:       2580562C9D       AND EAX, 2636928640
;      0D6:       31C2             XOR EDX, EAX
;      0D8:       8BC2             MOV EAX, EDX
;      0DA:       C1E00F           SHL EAX, 15
;      0DD:       250000C6EF       AND EAX, 4022730752
;      0E2:       31C2             XOR EDX, EAX
;      0E4:       8BC2             MOV EAX, EDX
;      0E6:       C1E812           SHR EAX, 18
;      0E9:       31C2             XOR EDX, EAX
;      0EB:       B936000000       MOV ECX, 54
;      0F0:       8BC2             MOV EAX, EDX
;      0F2:       F7E1             MUL EAX, ECX
;      0F4:       8B051C60F123     MOV EAX, [#x23F1601C]      ; "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjklmopqrst"
;      0FA:       8B449001         MOV EAX, [EAX+EDX*4+1]
;      0FE:       8B4DF8           MOV ECX, [EBP-8]
;      101:       89440E01         MOV [ESI+ECX+1], EAX
;      105:       8B5DF8           MOV EBX, [EBP-8]
;      108:       83C304           ADD EBX, 4
;      10B:       895DF8           MOV [EBP-8], EBX
;      10E: L5:   397DF8           CMP [EBP-8], EDI
;      111:       7C84             JL L3
;      113:       8BD6             MOV EDX, ESI
;      115:       8BE5             MOV ESP, EBP
;      117:       F8               CLC
;      118:       5D               POP EBP
;      119:       C3               RET
NIL

I could probably optimize this more, but why? It's already fast enough, and premature optimization is stupid. When I need speed, I just run a profiler and improve the code in a few functions that take most of the time, while leaving the rest as it is. This can substantially improve the whole program's speed(unless it's something that has the code paths very uniformly distributed) and is much smarter than prematurely optimizing everything.

Name: Anonymous 2010-01-12 16:51

(time (let loop ((n 100000)
                   (dicks 'cocks))
          (if (zero? n)
              dicks
              (loop (- n 1) (list->string (build-list 100 (λ(x) (integer->char (+ 48 (random 74))))))))))
cpu time: 4891 real time: 4953 gc time: 265
"tK6YeD:kp0nGvw76=o50oyRWqDjZhqT:T8RBr;8r=`y@MWH6roBqeVtEY[;T3yHLdtHSXn5?NT3rPN5_ldw;FICK2L99Ng?G\\hpD"


Fucking SLOW AS FUCK. Five seconds to generate and throw away 100,000 100-character strings? Who has that kind of time?

Time to get down to the fucking metal to tackle this problem.

Name: Anonymous 2010-01-12 17:40

misaka@kuroko:~$ cat randtest.c
#include <stdio.h>

void randtext(char *out, int len) {
 static char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz";
 while(len--)
  *out++ = chars[rand()%36];
}

char buf[100];
int main() {
 int i;
 for(i=0;i<100000;i++)
  randtext(buf,100);
 return 0;
}
misaka@kuroko:~$ cc -O2 -o randtest randtest.c
misaka@kuroko:~$ time ./randtest

real    0m0.018s
user    0m0.017s
sys     0m0.000s
misaka@kuroko:~$

Name: Anonymous 2010-01-12 17:50

This shouldn't take more than ~10-15 cycles per character.

Name: Anonymous 2010-01-12 17:56

I M LARNIN TO SCEME
(define all "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
(define len (string-length all))

(define (jib x)
  (define (jab n cs)
    (if (= n 0)
        cs
        (jab (- n 1) (cons (string-ref all (random len)) cs))))
  (jab (* x 100) '()))

(list->string (jiv 100000))

takes about 550ms in Ikarus
(while the exact same thing, with all defined as a list of chars and using list-ref runs around 2000ms. Wtf.)

Name: Anonymous 2010-01-12 19:03

>>16
In general, you should use higher-order functions like foldl and foldr instead of writing out the recursion. Various SRFIs provide these facilities for other data types (strings,vectors...) and you should use them.
(while the exact same thing, with all defined as a list of chars and using list-ref runs around 2000ms. Wtf.)
list-ref is O(n), whereas any sane implementation of string-ref is O(1).

My scheme solution (mzscheme, but your scheme probably provides an implementation of SRFI 13 for string-tabulate)

(require srfi/13)

(define random-character
  (let* ((valid-characters "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
        (len (string-length valid-characters)))
    (lambda ()
      (string-ref valid-characters (random len)))))

(define (gibberish length)
  (string-tabulate (lambda (x) (random-character)) length))


and the time taken (in milliseconds)
> (time (for ((i (in-range 1 100000))) (gibberish 100)))
cpu time: 5033 real time: 5189 gc time: 24

Name: Anonymous 2010-01-12 19:12

>>14
That's quite fast, probably because there's no malloc/free overhead.

Name: >>17 2010-01-12 19:22

I meant to say this in my post, but can someone check the time on a "modern computer", the one I'm currently sitting at is 5 years old and has a whopping 1GB of RAM. I don't expect a significant speed up, just curious.

Name: Anonymous 2010-01-13 1:57

>>19
I got it to run in about 3.9 seconds.

(time (for ((i (in-range 1 100000))) (gibberish 100)))
cpu time: 3873 real time: 3874 gc time: 16
(time (for ((i (in-range 1 100000))) (gibberish 100)))
cpu time: 3924 real time: 3935 gc time: 4
(time (for ((i (in-range 1 100000))) (gibberish 100)))
cpu time: 3912 real time: 3911 gc time: 12

Name: Anonymous 2010-01-13 6:50

>>20
ty

Name: Anonymous 2010-01-13 10:28

newLISP

(select "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" (rand 62 10000000))

Name: Anonymous 2010-01-13 11:30

>>4
base64 != alphanumeric

Name: Anonymous 2010-01-13 11:31

openBinaryFile "/dev/random" ReadMode >>= hGetContents >>= fmap (filter $ flip elem "0123456789abcdefghijklmnopqrstuvwxyz")

Name: Anonymous 2010-01-13 12:08

>>22
oldLISP just got told.

Name: Anonymous 2010-01-15 21:59

ENTERPRISE C IMPLEMENTATION

#include <stdio.h>
#include <time.h>

char characters[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjklmopqrst";
char gibberish[1];

int main()
{
    srand(time(NULL));
    int x;
   
    for (x = 0; x <= 300; x++) gibberish[x] = characters[rand() % 54];
    for (x = 0; x <= 300; x++) printf("%c", gibberish[x]);
   
    return 0;
}

Name: Anonymous 2010-01-16 2:32

>>26
does that work?

i'm just learning c, but it seems to me that you'd be running out of the bounds of your gibberish array in the first for loop. that's ok?

also, why not just

printf("%c", characters[rand() % 54]);

or

for (int x = 0; x < = 300; ++x) {
    gibberish[0] = characters[rand() % 54];
    print("%c", gibberish[0]);
}


?

Name: Anonymous 2010-01-16 2:33

>>27
s/print/printf

Name: Anonymous 2010-01-16 4:04

>>1
Hire a Perl coder.

Name: Anonymous 2010-01-16 8:51

>>26
I admire your enterprise skills. I assume you were badass enough to run that code?

Name: Anonymous 2010-01-16 14:10

>>26 disregard that, I suck cocks.

#include <stdio.h>
#include <time.h>

int main()
{
    char characters[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    char gibberish[300];
    int x;
   
    srand(time(NULL));
    for (x = 0; x < 300; x++) gibberish[x] = characters[rand() % 62];
    for (x = 0; x < 300; x++) printf("%c", gibberish[x]);
   
    return 0;
}


>>27

It shouldn't have run, I'm just an idiot. Also, your solution is much preferable to mine, I must say, saving an array and a for loop.

#include <stdio.h>
#include <time.h>

int main()
{
    char characters[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    int x;
   
    srand(time(NULL));
    for (x = 0; x < 300; x++) printf("%c", characters[rand() % 62]);
   
    return 0;
}

Name: Anonymous 2011-02-04 19:46

Name: Anonymous 2011-02-18 13:07

check my doubles
Don't change these.
Name: Email:
Entire Thread Thread List