# package rand
`import "crypto/rand"`
rand包实现了用于加解密的更安全的随机数生成器。
## Index
* [Variables](#pkg-variables)
* [func Int(rand io.Reader, max \*big.Int) (n \*big.Int, err error)](#Int)
* [func Prime(rand io.Reader, bits int) (p \*big.Int, err error)](#Prime)
* [func Read(b []byte) (n int, err error)](#Read)
### Examples
* [Read](#example-Read)
## Variables
```
var Reader io.Reader
```
Reader是一个全局、共享的密码用强随机数生成器。在Unix类型系统中,会从/dev/urandom读取;而Windows中会调用CryptGenRandom API。
## func [Int](https://github.com/golang/go/blob/master/src/crypto/rand/util.go#L106 "View Source")
```
func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)
```
返回一个在[0, max)区间服从均匀分布的随机值,如果max<=0则会panic。
## func [Prime](https://github.com/golang/go/blob/master/src/crypto/rand/util.go#L31 "View Source")
```
func Prime(rand io.Reader, bits int) (p *big.Int, err error)
```
返回一个具有指定字位数的数字,该数字具有很高可能性是质数。如果从rand读取时出错,或者bits<2会返回错误。
## func [Read](https://github.com/golang/go/blob/master/src/crypto/rand/rand.go#L19 "View Source")
```
func Read(b []byte) (n int, err error)
```
本函数是一个使用io.ReadFull调用Reader.Read的辅助性函数。当且仅当err == nil时,返回值n == len(b)。
Example
```
c := 10
b := make([]byte, c)
_, err := rand.Read(b)
if err != nil {
fmt.Println("error:", err)
return
}
// The slice should now contain random bytes instead of only zeroes.
fmt.Println(bytes.Equal(b, make([]byte, c)))
```
Output:
```
false
```
- 库
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe