github.com/zhongdalu/gf@v1.0.0/g/util/grand/grand.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  // Package grand provides high performance random string generation functionality.
     8  package grand
     9  
    10  var (
    11  	letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
    12  	digits  = []rune("0123456789")
    13  )
    14  
    15  // Meet randomly calculate whether the given probability <num>/<total> is met.
    16  func Meet(num, total int) bool {
    17  	return Intn(total) < num
    18  }
    19  
    20  // MeetProb randomly calculate whether the given probability is met.
    21  func MeetProb(prob float32) bool {
    22  	return Intn(1e7) < int(prob*1e7)
    23  }
    24  
    25  // N returns a random int between min and max - [min, max].
    26  func N(min, max int) int {
    27  	if min >= max {
    28  		return min
    29  	}
    30  	if min >= 0 {
    31  		// Because Intn dose not support negative number,
    32  		// so we should first shift the value to left,
    33  		// then call Intn to produce the random number,
    34  		// and finally shift the result to right.
    35  		return Intn(max-(min-0)+1) + (min - 0)
    36  	}
    37  	if min < 0 {
    38  		// Because Intn dose not support negative number,
    39  		// so we should first shift the value to right,
    40  		// then call Intn to produce the random number,
    41  		// and finally shift the result to left.
    42  		return Intn(max+(0-min)+1) - (0 - min)
    43  	}
    44  	return 0
    45  }
    46  
    47  // Deprecated.
    48  // Alias of N.
    49  func Rand(min, max int) int {
    50  	return N(min, max)
    51  }
    52  
    53  // Str returns a random string which contains digits and letters, and its length is <n>.
    54  func Str(n int) string {
    55  	b := make([]rune, n)
    56  	for i := range b {
    57  		if Intn(2) == 1 {
    58  			b[i] = digits[Intn(10)]
    59  		} else {
    60  			b[i] = letters[Intn(52)]
    61  		}
    62  	}
    63  	return string(b)
    64  }
    65  
    66  // Deprecated.
    67  // Alias of Str.
    68  func RandStr(n int) string {
    69  	return Str(n)
    70  }
    71  
    72  // Digits returns a random string which contains only digits, and its length is <n>.
    73  func Digits(n int) string {
    74  	b := make([]rune, n)
    75  	for i := range b {
    76  		b[i] = digits[Intn(10)]
    77  	}
    78  	return string(b)
    79  
    80  }
    81  
    82  // Deprecated.
    83  // Alias of Digits.
    84  func RandDigits(n int) string {
    85  	return Digits(n)
    86  }
    87  
    88  // Letters returns a random string which contains only letters, and its length is <n>.
    89  func Letters(n int) string {
    90  	b := make([]rune, n)
    91  	for i := range b {
    92  		b[i] = letters[Intn(52)]
    93  	}
    94  	return string(b)
    95  
    96  }
    97  
    98  // Deprecated.
    99  // Alias of Letters.
   100  func RandLetters(n int) string {
   101  	return Letters(n)
   102  }
   103  
   104  // Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).
   105  func Perm(n int) []int {
   106  	m := make([]int, n)
   107  	for i := 0; i < n; i++ {
   108  		j := Intn(i + 1)
   109  		m[i] = m[j]
   110  		m[j] = i
   111  	}
   112  	return m
   113  }