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

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.

主站蜘蛛池模板: 仙游县| 瓦房店市| 天柱县| 庄浪县| 偃师市| 靖州| 孝感市| 曲水县| 铜陵市| 称多县| 龙游县| 浏阳市| 黄大仙区| 横峰县| 轮台县| 长兴县| 诏安县| 岳池县| 江源县| 许昌县| 龙泉市| 德惠市| 浙江省| 东宁县| 平和县| 饶河县| 乐山市| 红原县| 大田县| 曲沃县| 门源| 曲靖市| 大丰市| 来安县| 泽普县| 奇台县| 聂拉木县| 惠东县| 泰州市| 日土县| 南昌县|