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

  • 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.

主站蜘蛛池模板: 上蔡县| 新平| 和田市| 库伦旗| 正蓝旗| 台东县| 财经| 湾仔区| 江安县| 宜昌市| 梨树县| 大英县| 视频| 赤壁市| 清涧县| 新干县| 红原县| 沙坪坝区| 获嘉县| 高陵县| 涡阳县| 昌乐县| 鹤庆县| 泰来县| 长沙市| 资中县| 临夏县| 全椒县| 成都市| 乐都县| 丹棱县| 岚皋县| 汶上县| 景德镇市| 思茅市| 洱源县| 改则县| 鄂州市| 卢湾区| 张家川| 万山特区|