random

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: Apache-2.0, MIT Imports: 13 Imported by: 15

Documentation

Overview

Package random provides functionality to generate pseudo-random test data.

All random numbers and data are created deterministically using the ChaCha8 pseudo-random number generator. This generator's output is determined by a seed value that can be set explicitly. If not set, then cryptographically secure random data is used as the seed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddrInfos added in v0.3.0

func AddrInfos(numPeers, numAddrs int) []peer.AddrInfo

AddrInfos returns a slice AddrInfo with numPeers elements. Each AddrInfo has a unique ID and numAddrs Addresses. The multiaddrs are ipv4 addresses.

Creates its own random source; safe for concurrent use.

func Addrs

func Addrs(n int) []string

Addrs returns a slice of n random unique IPv4 addresses.

Creates its own random source; safe for concurrent use.

func BlocksOfSize

func BlocksOfSize(n int, size int64) []blocks.Block

BlocksOfSize generates a slice of blocks of the specified byte size.

Creates its own random source; safe for concurrent use.

func Bytes

func Bytes(n int64) []byte

Bytes returns a byte array of the given size with random values.

Creates its own random source; safe for concurrent use.

func Cids

func Cids(n int) []cid.Cid

Cids returns a slice of n random unique CIDs.

Creates its own random source; safe for concurrent use.

func DnsAddrInfos added in v0.3.0

func DnsAddrInfos(numPeers, numAddrs int) []peer.AddrInfo

DnsAddrInfos returns a slice AddrInfo with numPeers elements. Each AddrInfo has a unique ID and numAddrs Addresses. The multiaddrs are dns addresses.

Creates its own random source; safe for concurrent use.

func DnsAddrs added in v0.3.0

func DnsAddrs(n int) []string

DnsAddrs returns a slice of n random unique DNS addresses in the format "xxxxxxxx.example.com:port".

Creates its own random source; safe for concurrent use.

func DnsMultiaddrs added in v0.3.0

func DnsMultiaddrs(n int) []multiaddr.Multiaddr

DnsMultiaddrs returns a slice of n random unique Multiaddrs with dns addresses.

Creates its own random source; safe for concurrent use.

func HttpAddrInfos added in v0.3.0

func HttpAddrInfos(numPeers, numAddrs int) []peer.AddrInfo

HttpAddrInfos returns a slice AddrInfo with numPeers elements. Each AddrInfo has a unique ID and numAddrs Addresses. The multiaddrs are ipv4 addresses with http protocol

Creates its own random source; safe for concurrent use.

func HttpDnsAddrInfos added in v0.3.0

func HttpDnsAddrInfos(numPeers, numAddrs int) []peer.AddrInfo

HttpDnsAddrInfos returns a slice AddrInfo with numPeers elements. Each AddrInfo has a unique ID and numAddrs Addresses. The multiaddrs are dns addresses with http protocol.

Creates its own random source; safe for concurrent use.

func HttpDnsMultiaddrs added in v0.3.0

func HttpDnsMultiaddrs(n int) []multiaddr.Multiaddr

HttpDnsMultiaddrs returns a slice of n random unique Multiaddrs with dns addresses and http protocol.

Creates its own random source; safe for concurrent use.

func HttpMultiaddrs

func HttpMultiaddrs(n int) []multiaddr.Multiaddr

HttpMultiaddrs returns a slice of n random unique Multiaddrs with ipv4 addresses and http protocol.

Creates its own random source; safe for concurrent use.

func Identity

func Identity() (peer.ID, crypto.PrivKey, crypto.PubKey)

Identity returns a random unique peer ID, private key, and public key.

Creates its own random source; safe for concurrent use.

func Multiaddrs

func Multiaddrs(n int) []multiaddr.Multiaddr

Multiaddrs returns a slice of n random unique Multiaddrs with ipv4 addresses.

Creates its own random source; safe for concurrent use.

func Multihashes

func Multihashes(n int) []multihash.Multihash

Multihashes returns a slice of n random unique Multihashes.

Creates its own random source; safe for concurrent use.

func Name added in v0.4.0

func Name(size int) string

Name returns a string of n random lower-alphanumeric characters.

Creates its own random source; safe for concurrent use.

func NameSize added in v0.4.0

func NameSize(minSize, maxSize int) string

NameSize returns a string of random lower-alphanumeric characters, having a random size of at least minSize and at most maxSize.

Creates its own random source; safe for concurrent use.

func NewSeed added in v0.4.0

func NewSeed() [32]byte

NewSeed creates a new random seed for use in seeding creating random sources with the same seed value using NewSeeded.

func Peers

func Peers(n int) []peer.ID

Peers returns a slice of n random peer IDs.

Creates its own random source; safe for concurrent use.

func StringToSeed added in v0.4.0

func StringToSeed(s string) [32]byte

StringToSeed creates a seed value from a string. The string is hashed so that if the string is longer than the necessary seed data, the entire string contributes to the seed value.

func Uint64ToSeed added in v0.4.0

func Uint64ToSeed(val uint64) [32]byte

Uint64ToSeed creates a seed value from a uint64 value.

Types

type Random added in v0.4.0

type Random struct {
	*rand.Rand
	// contains filtered or unexported fields
}

Random extends math/rand/v2 Rand with functions for generating many useful types of data. It also provides a Read function to read random data into a buffer and serve as an io.Reader. Random contains its own pseudo-random source. The same Random instance must not be used concurrently. Create separate Random instances for use in concurrent goroutines.

func New added in v0.4.0

func New() *Random

New returns a new Random instance that contains its own pseudo-random number source, seeded with a random value.

func NewRand deprecated added in v0.0.4

func NewRand() *Random

Deprecated: use New()

func NewSeeded added in v0.4.0

func NewSeeded(seed [32]byte) *Random

NewSeeded returns a new Random instance that contains its own pseudo-random number source seeded with the specified value. Creating a random source using the same seed value causes the generator to deterministically produce the same sequence of pseudo-random numbers.

func NewSeededRand deprecated added in v0.0.4

func NewSeededRand(seed uint64) *Random

Deprecated: use NewSeeded(bytesSeed) or NewSeeded(MakeSeed(uint64Seed))

func (*Random) AddrInfos added in v0.4.0

func (r *Random) AddrInfos(numPeers, numAddrs int) []peer.AddrInfo

AddrInfos returns a slice AddrInfo with numPeers elements. Each AddrInfo has a unique ID and numAddrs Addresses. The multiaddrs are ipv4 addresses.

func (*Random) Addrs added in v0.4.0

func (r *Random) Addrs(n int) []string

Addrs returns a slice of n random unique IPv4 addresses with a random port. The string is formatted as a multiaddr: "/ip4/n.n.n.n/tcp/n"

func (*Random) BlocksOfSize added in v0.4.0

func (r *Random) BlocksOfSize(n int, size int64) []blocks.Block

BlocksOfSize generates a slice of blocks of the specified byte size.

func (*Random) Bytes added in v0.4.0

func (r *Random) Bytes(n int64) []byte

Bytes returns a byte array of the given size with random values.

func (*Random) Cids added in v0.4.0

func (r *Random) Cids(n int) []cid.Cid

Cids returns a slice of n random unique CIDs.

func (*Random) DnsAddrInfos added in v0.4.0

func (r *Random) DnsAddrInfos(numPeers, numAddrs int) []peer.AddrInfo

DnsAddrInfos returns a slice AddrInfo with numPeers elements. Each AddrInfo has a unique ID and numAddrs Addresses. The multiaddrs are dns addresses.

func (*Random) DnsAddrs added in v0.4.0

func (r *Random) DnsAddrs(n int) []string

DnsAddrs returns a slice of n random unique DNS addresses, formmated as a multiaddr: "/dns4/xxxxxxxx.example.com/tcp/n"

func (*Random) DnsMultiaddrs added in v0.4.0

func (r *Random) DnsMultiaddrs(n int) []multiaddr.Multiaddr

DnsMultiaddrs returns a slice of n random unique Multiaddrs with dns addresses.

func (*Random) HttpAddrInfos added in v0.4.0

func (r *Random) HttpAddrInfos(numPeers, numAddrs int) []peer.AddrInfo

HttpAddrInfos returns a slice AddrInfo with numPeers elements. Each AddrInfo has a unique ID and numAddrs Addresses. The multiaddrs are ipv4 addresses with http protocol

func (*Random) HttpDnsAddrInfos added in v0.4.0

func (r *Random) HttpDnsAddrInfos(numPeers, numAddrs int) []peer.AddrInfo

HttpDnsAddrInfos returns a slice AddrInfo with numPeers elements. Each AddrInfo has a unique ID and numAddrs Addresses. The multiaddrs are dns addresses with http protocol.

func (*Random) HttpDnsMultiaddrs added in v0.4.0

func (r *Random) HttpDnsMultiaddrs(n int) []multiaddr.Multiaddr

HttpDnsMultiaddrs returns a slice of n random unique Multiaddrs with dns addresses and http protocol.

func (*Random) HttpMultiaddrs added in v0.4.0

func (r *Random) HttpMultiaddrs(n int) []multiaddr.Multiaddr

HttpMultiaddrs returns a slice of n random unique Multiaddrs with ipv4 addresses and http protocol.

func (*Random) Identity added in v0.4.0

func (r *Random) Identity() (peer.ID, crypto.PrivKey, crypto.PubKey)

Identity returns a random unique peer ID, private key, and public key.

func (*Random) Multiaddrs added in v0.4.0

func (r *Random) Multiaddrs(n int) []multiaddr.Multiaddr

Multiaddrs returns a slice of n random unique Multiaddrs with ipv4 addresses.

func (*Random) Multihashes added in v0.4.0

func (r *Random) Multihashes(n int) []multihash.Multihash

Multihashes returns a slice of n random unique Multihashes.

func (*Random) Name added in v0.4.0

func (r *Random) Name(size int) string

Name returns a string of n random lower-alphanumeric characters.

func (*Random) NameSize added in v0.4.0

func (r *Random) NameSize(minSize, maxSize int) string

NameSize returns a string of random lower-alphanumeric characters, having a random size of at least minSize and at most maxSize.

func (*Random) Peers added in v0.4.0

func (r *Random) Peers(n int) []peer.ID

Peers returns a slice of n random peer IDs.

func (*Random) Read added in v0.4.0

func (r *Random) Read(p []byte) (n int, err error)

Read reads exactly len(p) bytes into p. It always returns len(p) and a nil error.

func (*Random) Seed added in v0.4.0

func (r *Random) Seed(seed [32]byte)

Seed resets the Random instance to behave the same way as NewSeeded(seed).

type Sequence added in v0.0.3

type Sequence struct {
	// contains filtered or unexported fields
}

Sequence returns a series of monotonically increasing numbers, starting at the next unique global sequence value. Any current calls to Sequence will not generate any overlapping values.

The sequence numbers themselves are not random, only the global starting value of the sequence numbers is random. This ensures that all sequences generated within a test are unique, assuming < 2^64 values are generated, but start out at a random value.

func NewSequence added in v0.4.0

func NewSequence() *Sequence

NewSequence creates a new Sequence that starts at a random number.

func NewSequenceAt added in v0.4.0

func NewSequenceAt(start uint64) *Sequence

NewSequenceAt creates a new Sequence that starts at a the specified number.

func (*Sequence) Next added in v0.4.0

func (s *Sequence) Next() uint64

Next returns the next sequence value. This is equivalent to NextN(1)[0].

func (*Sequence) NextN added in v0.4.0

func (s *Sequence) NextN(n int) []uint64

NextN returns the next n sequence values.

Directories

Path Synopsis
Package files provides functionality for creating filesystem hierarchies of random files an directories.
Package files provides functionality for creating filesystem hierarchies of random files an directories.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL