Golang源码包剖析--math/rand

概述

Golang的版本信息

go version go1.13.4 darwin/amd64

简介

Package rand implements pseudo-random number generators.

Random numbers are generated by a Source. Top-level functions, such as Float64 and Int, use a default shared Source that produces a deterministic sequence of values each time a program is run. Use the Seed function to initialize the default Source if different behavior is required for each run. The default Source is safe for concurrent use by multiple goroutines, but Sources created by NewSource are not.

Mathematical interval notation such as [0, n) is used throughout the documentation for this package.

For random numbers suitable for security-sensitive work, see the crypto/rand package.

rand包实现了伪随机数生成器。随机数由源(Source)生成,顶级函数,如Float64Int使用一个默认的共享源,该源在每次运行程序时都会产生确定的值序列。如果每次运行时需要不同的行为,可以使用Seed函数初始化默认源。默认的Source可安全地供多个goroutine并发使用,但不适用于用NewSource函数创建的Source。有关适合于安全敏感工作的真随机数,请参考crypto/rand软件包。

-- 随机性 不可预测性 不可重现性 用途 备注
弱伪随机数 没有 没有 不可用于密码技术 不带种子函数时调用math/rand
强伪随机数 没有 可用于密码技术 带种子函数时调用math/rand
真随机数 可用于密码技术 直接调用crypto/rand

源码解析

源码包目录解析

rand
│   example_test.go
│   exp.go  
│   gen_cooked.go 
│   normal.go
│   race_test.go
│   rand.go
│   rand_test.go
│   regress_test.go
│   rng.go
│   zipf.go
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。