github.com/webx-top/com@v1.2.12/rand.go (about)

     1  package com
     2  
     3  import (
     4  	cryptoRand "crypto/rand"
     5  	"math"
     6  	"math/rand"
     7  	"time"
     8  )
     9  
    10  // RandomSpec0 Creates a random string based on a variety of options, using
    11  // supplied source of randomness.
    12  //
    13  // If start and end are both 0, start and end are set
    14  // to ' ' and 'z', the ASCII printable
    15  // characters, will be used, unless letters and numbers are both
    16  // false, in which case, start and end are set to 0 and math.MaxInt32.
    17  //
    18  // If set is not nil, characters between start and end are chosen.
    19  //
    20  // This method accepts a user-supplied rand.Rand
    21  // instance to use as a source of randomness. By seeding a single
    22  // rand.Rand instance with a fixed seed and using it for each call,
    23  // the same random sequence of strings can be generated repeatedly
    24  // and predictably.
    25  func randomSpec0(count uint, start, end int, letters, numbers bool,
    26  	chars []rune) string {
    27  	if count == 0 {
    28  		return ""
    29  	}
    30  	if start == 0 && end == 0 {
    31  		end = 'z' + 1
    32  		start = ' '
    33  		if !letters && !numbers {
    34  			start = 0
    35  			end = math.MaxInt32
    36  		}
    37  	}
    38  	buffer := make([]rune, count)
    39  	gap := end - start
    40  	for count != 0 {
    41  		count--
    42  		var ch rune
    43  		if len(chars) == 0 {
    44  			ch = rune(rand.Intn(gap) + start)
    45  		} else {
    46  			ch = chars[rand.Intn(gap)+start]
    47  		}
    48  		if letters && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) ||
    49  			numbers && (ch >= '0' && ch <= '9') ||
    50  			(!letters && !numbers) {
    51  			if ch >= rune(56320) && ch <= rune(57343) {
    52  				if count == 0 {
    53  					count++
    54  				} else {
    55  					buffer[count] = ch
    56  					count--
    57  					buffer[count] = rune(55296 + rand.Intn(128))
    58  				}
    59  			} else if ch >= rune(55296) && ch <= rune(56191) {
    60  				if count == 0 {
    61  					count++
    62  				} else {
    63  					// high surrogate, insert low surrogate before putting it in
    64  					buffer[count] = rune(56320 + rand.Intn(128))
    65  					count--
    66  					buffer[count] = ch
    67  				}
    68  			} else if ch >= rune(56192) && ch <= rune(56319) {
    69  				// private high surrogate, no effing clue, so skip it
    70  				count++
    71  			} else {
    72  				buffer[count] = ch
    73  			}
    74  		} else {
    75  			count++
    76  		}
    77  	}
    78  	return string(buffer)
    79  }
    80  
    81  // RandomSpec1 Creates a random string whose length is the number of characters specified.
    82  //
    83  // Characters will be chosen from the set of alpha-numeric
    84  // characters as indicated by the arguments.
    85  //
    86  // Param count - the length of random string to create
    87  // Param start - the position in set of chars to start at
    88  // Param end   - the position in set of chars to end before
    89  // Param letters - if true, generated string will include
    90  //
    91  //	alphabetic characters
    92  //
    93  // Param numbers - if true, generated string will include
    94  //
    95  //	numeric characters
    96  func randomSpec1(count uint, start, end int, letters, numbers bool) string {
    97  	return randomSpec0(count, start, end, letters, numbers, nil)
    98  }
    99  
   100  // RandomAlphaOrNumeric Creates a random string whose length is the number of characters specified.
   101  //
   102  // Characters will be chosen from the set of alpha-numeric
   103  // characters as indicated by the arguments.
   104  //
   105  // Param count - the length of random string to create
   106  // Param letters - if true, generated string will include
   107  //
   108  //	alphabetic characters
   109  //
   110  // Param numbers - if true, generated string will include
   111  //
   112  //	numeric characters
   113  func randomAlphaOrNumeric(count uint, letters, numbers bool) string {
   114  	return randomSpec1(count, 0, 0, letters, numbers)
   115  }
   116  
   117  func RandomString(count uint) string {
   118  	return randomSpec1(count, 0, 255, false, false)
   119  }
   120  
   121  func RandomStringSpec0(count uint, set []rune) string {
   122  	return randomSpec0(count, 0, len(set)-1, false, false, set)
   123  }
   124  
   125  func RandomStringSpec1(count uint, set string) string {
   126  	return RandomStringSpec0(count, []rune(set))
   127  }
   128  
   129  // RandomASCII Creates a random string whose length is the number of characters
   130  // specified.
   131  // Characters will be chosen from the set of characters whose
   132  // ASCII value is between 32 and 126 (inclusive).
   133  func RandomASCII(count uint) string {
   134  	return randomSpec1(count, 32, 127, false, false)
   135  }
   136  
   137  // RandomAlphabetic Creates a random string whose length is the number of characters specified.
   138  // Characters will be chosen from the set of alphabetic characters.
   139  func RandomAlphabetic(count uint) string {
   140  	return randomAlphaOrNumeric(count, true, false)
   141  }
   142  
   143  // RandomAlphanumeric Creates a random string whose length is the number of characters specified.
   144  // Characters will be chosen from the set of alpha-numeric characters.
   145  func RandomAlphanumeric(count uint) string {
   146  	return randomAlphaOrNumeric(count, true, true)
   147  }
   148  
   149  // RandomNumeric Creates a random string whose length is the number of characters specified.
   150  // Characters will be chosen from the set of numeric characters.
   151  func RandomNumeric(count uint) string {
   152  	return randomAlphaOrNumeric(count, false, true)
   153  }
   154  
   155  // RandStr .
   156  func RandStr(count int) (r string) {
   157  	//count := 64
   158  	b := make([]byte, count)
   159  	_, err := cryptoRand.Read(b)
   160  	if err != nil {
   161  		r = RandomString(uint(count))
   162  	} else {
   163  		r = string(b)
   164  	}
   165  	return
   166  }
   167  
   168  // RandInt Get in the range [0, max], a random integer type int
   169  func RandInt(max int) int {
   170  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   171  	return r.Intn(max)
   172  }
   173  
   174  // RandFloat32 获取范围为[0.0, 1.0],类型为float32的随机小数
   175  func RandFloat32() float32 {
   176  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   177  	return r.Float32()
   178  }
   179  
   180  // RandFloat64 获取范围为[0.0, 1.0],类型为float64的随机小数
   181  func RandFloat64() float64 {
   182  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   183  	return r.Float64()
   184  }
   185  
   186  // RandPerm 获取范围为[0,max],数量为max,类型为int的随机整数slice
   187  func RandPerm(max int) []int {
   188  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   189  	return r.Perm(max)
   190  }
   191  
   192  // RandRangeInt64 生成区间随机数
   193  // @param int64 min 最小值
   194  // @param int64 max 最大值
   195  // @return int64 生成的随机数
   196  func RandRangeInt64(min, max int64) int64 {
   197  	if min >= max || min == 0 || max == 0 {
   198  		return max
   199  	}
   200  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   201  	return r.Int63n(max-min) + min
   202  }
   203  
   204  // RandRangeInt 生成区间随机数
   205  // @param int min 最小值
   206  // @param int max 最大值
   207  // @return int 生成的随机数
   208  func RandRangeInt(min, max int) int {
   209  	if min >= max || min == 0 || max == 0 {
   210  		return max
   211  	}
   212  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   213  	return r.Intn(max-min) + min
   214  }