[go] crypto/rand 패키지의 시드(seed) 설정 방법
아래는 crypto/rand
의 시드 설정 방법에 대한 예제 코드입니다.
package main
import (
"crypto/rand"
"encoding/binary"
"fmt"
"math/rand"
"time"
)
func main() {
var seed int64
binary.Read(rand.Reader, binary.BigEndian, &seed)
rand.Seed(seed)
// 다음에 난수 생성을 위한 코드 작성
}
위의 예제 코드에서 crypto/rand.Reader
를 사용하여 안전한 난수를 생성하고 이를 rand.Seed
함수를 통해 시드로 설정합니다. 그 후에는 원하는 곳에서 rand.Intn
또는 다른 rand
패키지의 함수를 사용하여 안전한 난수를 생성할 수 있습니다.
더 자세한 내용은 공식 Go 문서 여기를 참고할 수 있습니다.