How to generate random strings?

5,895 views
Skip to first unread message

calif

unread,
Sep 13, 2010, 5:32:18 PM9/13/10
to golang-nuts
Hey!

How can I generate random strings in Go?

I need strings generator, which generates strings with printable bytes
- like a-zA-Z,0-9 and some like !,#,^ etc.

There is a one thing - strings can't be repeated.

How can I do this?

Thanks in advance for Your reply.
Bless.
calif

roger peppe

unread,
Sep 13, 2010, 5:38:25 PM9/13/10
to calif, golang-nuts
how long should the strings be?

ptolomy23

unread,
Sep 13, 2010, 6:00:33 PM9/13/10
to calif, golang-nuts
Sounds a lot like a homework assignment, but what the heck.. here's how I'd do it without much thought:

Rob 'Commander' Pike

unread,
Sep 13, 2010, 6:23:13 PM9/13/10
to ptolomy23, calif, golang-nuts
Why use a vector? You know the length, just allocate a slice and range
over its indices. Similarly, why use a buffer rather than a slice of
bytes?

-rob

ptolomy23

unread,
Sep 13, 2010, 6:35:55 PM9/13/10
to Rob 'Commander' Pike, calif, golang-nuts
Valid points.
I used vector and Buffer because I by default reach for containers that grow dynamically; no need to manage indexes or worry about whether the initial sizes are correct. I did realize that I could just use slices right before I sent, though.

It still might get into an infinite loop if you ask for more strings than are possible for a particular size.
It also could generate zero-length strings, and it doesn't use all printable characters.
Probably other issues.

fango

unread,
Sep 13, 2010, 8:12:36 PM9/13/10
to golang-nuts
printable rings like base64 or URL, so try `encoding` package. If you
don't need determinism, easier to use `crypto/rand` than `rand` for
byte array. Here's a demo:


package main

import (
"crypto/rand"
"encoding/base64"
"fmt"
)

const STRLEN = 80
func main(){
b := make([]byte, STRLEN)
rand.Read(b)
en := base64.StdEncoding // or URLEncoding
d := make([]byte, en.EncodedLen(len(b)))
en.Encode(d, b)
fmt.Printf("src=%s\ndst=%s\n", b, d)
}

calif

unread,
Sep 14, 2010, 8:22:56 AM9/14/10
to golang-nuts
Hmm, i think about strings generator like this:
http://pastie.org/1158004

How to write it? :)

Thanks in advance.

Archos

unread,
Sep 14, 2010, 2:41:41 PM9/14/10
to golang-nuts
Some times to learn something new you need to forget what has been
learned.
Forget Python

calif

unread,
Sep 14, 2010, 2:49:43 PM9/14/10
to golang-nuts
I forget Python, but i wanted to show you what i was talking about. :)

So, can somebody help me?

Thanks in advance.

ptolomy23

unread,
Sep 14, 2010, 3:37:02 PM9/14/10
to calif, golang-nuts
Ah. Your Python code lists all of the possible strings from the given alphabet of lengths 1..n.

So, in Go you might do something like (attempting to use "product" to keep it similar):

calif

unread,
Sep 14, 2010, 3:57:34 PM9/14/10
to golang-nuts
This is it!

Thank you very much.

Now i'm reading this code, and trying to understand it. :)

It's great!
Thanks

Sonia Keys

unread,
Sep 14, 2010, 5:05:38 PM9/14/10
to golang-nuts
On Sep 13, 6:35 pm, ptolomy23 <ptolom...@gmail.com> wrote:
> ...
> New version:http://pastie.org/1156941
> ....

I wrote something similar before seeing your code. I even had the
goto in there at one point, but replaced it with a retry count.

Anyway, http://pastie.org/1159175

fango

unread,
Sep 14, 2010, 9:54:11 PM9/14/10
to golang-nuts
Is the question about generating permutation of indexes to a string?
then http://cs.utsa.edu/~wagner/knuth/fasc2b.pdf has a thorough study.

Cheers,
Fango
Reply all
Reply to author
Forward
0 new messages