10 examples of 'golang random' in Go

Every line of 'golang random' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
3func random2(y int) int {
4 x := 6 //@diag("x", "x declared but not used")
5 return y
6}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

8func Random2(y int) int { //@Random2,mark(RandomParamY, "y")
9 return y //@godef("y", RandomParamY)
10}
65func (w Password) Random(s int) (string, error) {
66 b, err := w.RandomByte(s)
67 return base64.URLEncoding.EncodeToString(b), err
68}
178func (s *SnapContext) random(arr TallySlice, seed int64) {
179 if len(arr) <= 0 {
180 return
181 }
182 rand.Seed(seed)
183 for i := len(arr) - 1; i >= 0; i-- {
184 num := rand.Intn(len(arr))
185 arr[i], arr[num] = arr[num], arr[i]
186 }
187 return
188}
118func Random() byte {
119 rand.Seed(time.Now().UnixNano())
120 return byte(rand.Intn(256))
121}
577func (m *MonotonicEntropy) random() (inc uint64, err error) {
578 if m.inc <= 1 {
579 return 1, nil
580 }
581
582 // Fast path for using a underlying rand.Rand directly.
583 if m.rng != nil {
584 // Range: [1, m.inc)
585 return 1 + uint64(m.rng.Int63n(int64(m.inc))), nil
586 }
587
588 // bitLen is the maximum bit length needed to encode a value < m.inc.
589 bitLen := bits.Len64(m.inc)
590
591 // byteLen is the maximum byte length needed to encode a value < m.inc.
592 byteLen := uint(bitLen+7) / 8
593
594 // msbitLen is the number of bits in the most significant byte of m.inc-1.
595 msbitLen := uint(bitLen % 8)
596 if msbitLen == 0 {
597 msbitLen = 8
598 }
599
600 for inc == 0 || inc >= m.inc {
601 if _, err = io.ReadFull(m.Reader, m.rand[:byteLen]); err != nil {
602 return 0, err
603 }
604
605 // Clear bits in the first byte to increase the probability
606 // that the candidate is < m.inc.
607 m.rand[0] &= uint8(int(1<<msbitLen) - 1)
608
609 // Convert the read bytes into an uint64 with byteLen
610 // Optimized unrolled loop.
611 switch byteLen {
612 case 1:
613 inc = uint64(m.rand[0])
614 case 2:
615 inc = uint64(binary.LittleEndian.Uint16(m.rand[:2]))
616 case 3, 4:
617 inc = uint64(binary.LittleEndian.Uint32(m.rand[:4]))
618 case 5, 6, 7, 8:
619 inc = uint64(binary.LittleEndian.Uint64(m.rand[:8]))
620 }
621 }
622
623 // Range: [1, m.inc)
624 return 1 + inc, nil
625}
35func (p Palette) Random() color.NRGBA {
36 return p[rand.Intn(p.Len())]
37}
558func (ss Strings) Random(source rand.Source) string {
559 n := len(ss)
560
561 // Avoid the extra allocation.
562 if n < 1 {
563 return ""
564 }
565 if n < 2 {
566 return ss[0]
567 }
568 rnd := rand.New(source)
569 i := rnd.Intn(n)
570 return ss[i]
571}
238func randuint64(rnd random.Interface) uint64 {
239 return uint64(rnd.Int63())>>31 | uint64(rnd.Int63())<<32
240}
10func Random() abi.File {
11 return Reader(rand.Reader, "random")
12}

Related snippets