1

need to extract the random characters from string

here is what i got:

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  b := make([]byte, 1)
for i := range b {
    b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
fmt.Println(string(b))

but it always returns "X" but, i need to return every time new character using rand function. any help would be really appreciated. Thanks.

6
  • 3
    What did you try? What was the problem? Commented Aug 21, 2017 at 7:55
  • Thanks. just updated the query .. please suggest Commented Aug 21, 2017 at 8:04
  • Another thing to note is in Go we can have UTF-8 characters and one single byte is not enough to hold those characters. Commented Aug 21, 2017 at 8:26
  • Possible duplicate of How to generate a random string of a fixed length in golang? Commented Aug 21, 2017 at 8:34
  • no. it always gives the same output. plz see code here: gist.github.com/anonymous/a90bdbd0ec0770b373890286322d9063 Commented Aug 21, 2017 at 8:45

2 Answers 2

2

Start by seeding the pseudorandom number generator. For example,

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().UnixNano())
    const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    b := make([]byte, 7)
    for i := range b {
        b[i] = letterBytes[rand.Intn(len(letterBytes))]
    }
    fmt.Println(string(b))
}

Output:

jfXtySC

The Go Playground

About the Playground

In the playground the time begins at 2009-11-10 23:00:00 UTC (determining the significance of this date is an exercise for the reader). This makes it easier to cache programs by giving them deterministic output.

Therefore, in the Go playground, time.Now().UnixNano() always returns the same value. For a random seed value, run the code on your computer.


For any Unicode code point (Go rune),

package main

import (
    "fmt"
    "math/rand"
    "time"
)

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().UnixNano())
    chars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ世界!@=")
    r := make([]rune, 50)
    for i := range r {
        r[i] = chars[rand.Intn(len(chars))]
    }
    fmt.Println(string(r))
}

Output:

世QRYSp=@giJMIKly=tXRefjtVkeE!yHhTSQHvLyUYdRNIBbILW
Sign up to request clarification or add additional context in comments.

5 Comments

thanks. i added the code as you mentioned . but it always outputs "Bb"
@user2315104: See the comment about the Go plaground in my revised answer.
can this logic be applicable to special characters as well ?
@user2315104: Use rune instead of byte. See my revised answer.
0

You probably don't need cryto/rand. Just set the seed to something different each time. The math/rand package is deterministic with the same seed, so you just need to change the seed each time. Assuming this is not super secure, just add this line.

rand.Seed(time.Now().Unix())

Also note if you're using play.golang.org to test it that's not going to work because the time pkg doesn't work there. It'll work fine anywhere else.

If you actually want a random selection of characters though, use crypto/rand.Read NOT a homemade version - you can encode it if you need it within a given character range. Finally, don't roll your own crypto, just in case that's what you're doing here.

1 Comment

can this logic be applicable to special characters as well ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.