Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

randutil.String doesn't pick the last character in a set #1

Open
antifuchs opened this issue Sep 4, 2014 · 0 comments
Open

randutil.String doesn't pick the last character in a set #1

antifuchs opened this issue Sep 4, 2014 · 0 comments
Labels

Comments

@antifuchs
Copy link

I wanted to test for any biases the string generator might have; sadly, it looks like the generator is strongly biased against the last character in a set - it never gets picked.

I wrote this test program:

package main

import (
    "os"
    "strconv"

    "fmt"

    "github.com/jmcvetta/randutil"
)

const (
    PasswordCharacters = "}~"
)

func main() {
    howmany, err := strconv.Atoi(os.Args[1])
    if err != nil {
        fmt.Printf("Need a password length, error %s\n", err)
        os.Exit(1)
    }
    length, err := strconv.Atoi(os.Args[2])
    if err != nil {
        fmt.Printf("Need a password count, error %s\n", err)
        os.Exit(1)
    }

    charWeights := make(map[rune]int)
    for _, c := range PasswordCharacters {
        charWeights[c] = 0
    }

    for i := 0; i < howmany; i++ {
        onePass, err := randutil.String(length, PasswordCharacters)
        if err != nil {
            fmt.Printf("Got error generating pass: %v\n", err)
        }
        for _, c := range onePass {
            charWeights[c]++
        }
    }
    for _, c := range PasswordCharacters {
        count := charWeights[c]
        fmt.Printf("%d\t%c\n", count, c)
    }
}

Then I ran this:

go run ./main.go 45 200

and get this result:

9000    }
0       ~

-- Expanding the character set, it looks like it's selecting every character but the last one.

@jmcvetta jmcvetta added the bug label Sep 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants