go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/fsutil/random.go (about)

     1  // implementation from https://github.com/golang/go/blob/master/src/io/ioutil/tempfile.go
     2  // Copyright 2010 The Go Authors. All rights reserved.
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package fsutil
     7  
     8  import (
     9  	"os"
    10  	"strconv"
    11  	"sync"
    12  	"time"
    13  )
    14  
    15  // Random number state.
    16  // We generate random temporary file names so that there's a good
    17  // chance the file doesn't exist yet - keeps the number of tries in
    18  // TempFile to a minimum.
    19  var rand uint32
    20  var randmu sync.Mutex
    21  
    22  func reseed() uint32 {
    23  	return uint32(time.Now().UnixNano() + int64(os.Getpid()))
    24  }
    25  
    26  func NextRandom() string {
    27  	randmu.Lock()
    28  	r := rand
    29  	if r == 0 {
    30  		r = reseed()
    31  	}
    32  	r = r*1664525 + 1013904223 // constants from Numerical Recipes
    33  	rand = r
    34  	randmu.Unlock()
    35  	return strconv.Itoa(int(1e9 + r%1e9))[1:]
    36  }