mirror of
https://github.com/jbranchaud/til
synced 2026-01-20 15:38:02 +00:00
Compare commits
1 Commits
24bbe29f4b
...
376a88020b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
376a88020b |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1546 TILs and counting..._
|
_1545 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -406,7 +406,6 @@ _1546 TILs and counting..._
|
|||||||
- [Check If Cobra Flag Was Set](go/check-if-cobra-flag-was-set.md)
|
- [Check If Cobra Flag Was Set](go/check-if-cobra-flag-was-set.md)
|
||||||
- [Combine Two Slices](go/combine-two-slices.md)
|
- [Combine Two Slices](go/combine-two-slices.md)
|
||||||
- [Detect If Stdin Comes From A Redirect](go/detect-if-stdin-comes-from-a-redirect.md)
|
- [Detect If Stdin Comes From A Redirect](go/detect-if-stdin-comes-from-a-redirect.md)
|
||||||
- [Deterministically Seed A Random Number Generator](go/deterministically-seed-a-random-number-generator.md)
|
|
||||||
- [Do Something N Times](go/do-something-n-times.md)
|
- [Do Something N Times](go/do-something-n-times.md)
|
||||||
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
||||||
- [Format Date And Time With Time Constants](go/format-date-and-time-with-time-constants.md)
|
- [Format Date And Time With Time Constants](go/format-date-and-time-with-time-constants.md)
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
# Deterministically Seed A Random Number Generator
|
|
||||||
|
|
||||||
If you need a random number in Go, you can always reach for the various
|
|
||||||
functions in the `rand` package.
|
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
for range 5 {
|
|
||||||
roll := rand.Intn(6) + 1
|
|
||||||
fmt.Printf("- %d\n", roll)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Each time I run that, I get a random set of values. Often in programming, we
|
|
||||||
want some control over the randomness. We want to _seed_ the randomness so that
|
|
||||||
it is deterministic. We want random, but the kind of random where we know how
|
|
||||||
we got there.
|
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
seed := int64(123)
|
|
||||||
src := rand.NewSource(seed)
|
|
||||||
rng := rand.New(src)
|
|
||||||
|
|
||||||
for range 5 {
|
|
||||||
roll := rng.Intn(6) + 1
|
|
||||||
fmt.Printf("- %d\n", roll)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
In this second snippet, we create a `Source` with a specific seed value that we
|
|
||||||
can use with a custom `Rand` struct. We can then deterministically get random
|
|
||||||
numbers from it.
|
|
||||||
Reference in New Issue
Block a user