官术网_书友最值得收藏!

  • Go Systems Programming
  • Mihalis Tsoukalos
  • 490字
  • 2021-07-02 18:08:00

Creating random numbers

As a practical programming example, this section will talk about creating random numbers in Go. Random numbers have many uses, including the generation of good passwords as well as the creation of files with random data that can be used for testing other applications. However, bear in mind that usually programming languages generate pseudorandom numbers that approximate the properties of a true random number generator.

Go uses the math/rand package for generating random numbers and needs a seed to start producing random numbers. The seed is used for initializing the entire process and is extremely important because if you always start with the same seed, you will always get the same sequence of random numbers.

The random.go program has three main parts. The first part is the preamble of the program:

package main 
 
import ( 
   "fmt" 
   "math/rand" 
   "os" 
   "strconv" 
   "time" 
) 

The second part is the definition of the random() function that returns a random number each time it is called, using the rand.Intn() Go function:

func random(min, max int) int { 
   return rand.Intn(max-min) + min 
} 

The two parameters of the random() function define the lower and upper limits of the generated random number. The last part of random.go is the implementation of the main() function that is mainly used for calling the random() function:

func main() { 
   MIN := 0 
   MAX := 0 
   TOTAL := 0 
   if len(os.Args) > 3 { 
         MIN, _ = strconv.Atoi(os.Args[1]) 
         MAX, _ = strconv.Atoi(os.Args[2]) 
         TOTAL, _ = strconv.Atoi(os.Args[3]) 
   } else { 
         fmt.Println("Usage:", os.Args[0], "MIX MAX TOTAL") 
         os.Exit(-1) 
   } 
 
   rand.Seed(time.Now().Unix()) 
   for i := 0; i < TOTAL; i++ { 
         myrand := random(MIN, MAX) 
         fmt.Print(myrand) 
         fmt.Print(" ") 
   } 
   fmt.Println() 
} 

A big part of the main() function involves dealing with the reading of command-line arguments as integer numbers and printing a descriptive error message in case you did not get the correct number of command-line arguments. This is the standard practice that we will follow in this book. The random.go program uses the Unix epoch time as the seed for the random number generator by calling the time.Now().Unix() function. The important thing to remember is that you do not have to call rand.Seed() multiple times. Lastly, random.go does not examine the error variable returned by strconv.Atoi() to save book space, not because it is not necessary.

Executing random.go generates the following kind of output:

$ go run random.go 12 32 20
29 27 20 23 22 28 13 16 22 26 12 29 22 30 15 19 26 24 20 29
  

Should you wish to generate more secure random numbers in Go, you should use the crypto/rand package, which implements a cryptographically secure pseudorandom number generator. You can find more information about the crypto/rand package by visiting its documentation page at https://golang.org/pkg/crypto/rand/.

If you are really into random numbers, then the definitive reference to the theory of random numbers is the second volume of The Art of Computer Programming by Donald Knuth.

主站蜘蛛池模板: 中阳县| 嘉义县| 大丰市| 新竹市| 龙游县| 调兵山市| 中卫市| 建阳市| 黄骅市| 北碚区| 东乡族自治县| 沙洋县| 响水县| 瑞昌市| 贵南县| 于田县| 西盟| 射阳县| 屯门区| 额敏县| 伊吾县| 和林格尔县| 兰考县| 徐闻县| 尼勒克县| 石家庄市| 泰顺县| 阿拉善盟| 抚顺县| 桐庐县| 四平市| 申扎县| 涟源市| 阿瓦提县| 徐水县| 绥江县| 揭东县| 高唐县| 隆德县| 通许县| 道真|