randx

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: MIT Imports: 5 Imported by: 0

README

stringx

封装了随机数相关函数

Random with time.Now().UnixNano() seed.
  • RandInt [int.Min, int.Max] 包括负数
  • RandUint [uint.Min, uint.Max] 不包括负数
  • RandInt64 [int64.Min, int64.Max] 包括负数
  • RandUint64 [uint64.Min, uint64.Max] 不包括负数
  • RandInt32 [int32.Min, int32.Max] 包括负数
  • RandUint32 [uint32.Min, uint32.Max] 不包括负数

Random with system source(/dev/urandom), more security.

  • RandIntSecure [int.Min, int.Max] 包括负数
  • RandUintSecure [uint.Min, uint.Max] 不包括负数
  • RandInt64Secure [int64.Min, int64.Max] 包括负数
  • RandUint64Secure [uint64.Min, uint64.Max] 不包括负数
  • RandInt32Secure [int32.Min, int32.Max] 包括负数
  • RandUint32Secure [uint32.Min, uint32.Max] 不包括负数

Random in range.

Specific m and n(m <= n, otherwise will panic), random range in [m, n].

  • RandBetween use time.Now().UnixNano() as each random source.
  • SimpleRandBetween use global rand source, in each time call, time.Now().UnixNano() will feed in.
  • RandBetweenSecure feed system source(/dev/urandom) as each call source.

Shuffle

Shuffle slice.

  • Shuffle function to shuffle a slice.
  • SetShuffleRand set Random in range function.
    (Option1. RandBetween)
    (Option2. SimpleRandBetween)
    (Option3. RandBetweenSecure)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandBetween

func RandBetween(min int, max int) int

RandBetween : generate rand between [a, b], return int

func RandBetweenSecure

func RandBetweenSecure(min int, max int) int

RandBetweenSecure : generate rand between [a, b], return int

func RandInt

func RandInt() int

RandInt random generate int, rand seed is current nano timestamp

func RandInt32

func RandInt32() int32

RandInt32 random generate int32, rand seed is current nano timestamp

func RandInt32Secure

func RandInt32Secure() int32

RandInt32Secure random generate int32, read data from linux /dev/urandom actually, it's almost a random id

func RandInt64

func RandInt64() int64

RandInt64 random generate int64, rand seed is current nano timestamp

func RandInt64Secure

func RandInt64Secure() int64

RandInt64Secure random generate int64, read data from linux /dev/urandom actually, it's almost a random id

func RandIntSecure

func RandIntSecure() int

RandIntSecure random generate int, read data from linux /dev/urandom actually, it's almost a random id

func RandUint

func RandUint() uint

RandUint random generate uint, rand seed is current nano timestamp

func RandUint32

func RandUint32() uint32

RandUint32 random generate uint32, rand seed is current nano timestamp

func RandUint32Secure

func RandUint32Secure() uint32

RandUint32Secure random generate uint32, read data from linux /dev/urandom actually, it's almost a random id

func RandUint64

func RandUint64() uint64

RandUint64 random generate uint64, rand seed is current nano timestamp

func RandUint64Secure

func RandUint64Secure() uint64

RandUint64Secure random generate uint64, read data from linux /dev/urandom actually, it's almost a random id

func RandUintSecure

func RandUintSecure() uint

RandUintSecure random generate uint, read data from linux /dev/urandom actually, it's almost a random id

func SetShuffleRand

func SetShuffleRand(f func(min int, max int) int)

SetShuffleRand : set shuffle rand function

func Shuffle

func Shuffle(ss Swapper)

Shuffle : shuffle slice

func SimpleRandBetween

func SimpleRandBetween(min int, max int) int

SimpleRandBetween : generate rand between [a, b], use same global rand source: rand.Rand

Types

type Swapper

type Swapper interface {
	Swap(i, j int)
	Len() int
}

Swapper interface, each slice is a swapper, for example

type IntSwap []int
func (x IntSwap) (i, j) {
    x[i], x[j] = x[j], x[i]
}

func (x IntSwap) Len() int{
    return len(x)
}

Jump to

Keyboard shortcuts

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