summaryrefslogtreecommitdiff
path: root/content/posts/golang-generate-random-numbers.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/posts/golang-generate-random-numbers.md')
-rw-r--r--content/posts/golang-generate-random-numbers.md53
1 files changed, 53 insertions, 0 deletions
diff --git a/content/posts/golang-generate-random-numbers.md b/content/posts/golang-generate-random-numbers.md
new file mode 100644
index 0000000..88ab942
--- /dev/null
+++ b/content/posts/golang-generate-random-numbers.md
@@ -0,0 +1,53 @@
+---
+title: 'Golang: Generate Random Numbers'
+description: "Here's how to generate pseudorandom numbers in Go between to values. NOTE: You should always seed your random generator, or else it will produce the same result every time."
+date: 2021-08-13T11:18:04+02:00
+tags: ['go', 'golang']
+draft: false
+---
+
+Here's how to generate pseudorandom numbers in Go between to values. NOTE: You should always seed your random generator, or else it will produce the same result every time. Include this snippet at the top of your main func.
+
+<!--more-->
+
+```golang
+func main() {
+ rand.Seed(time.Now().UnixNano())
+
+ // ...
+}
+```
+
+## Generate a random int between two values
+
+This example returns a random integer between min and max, where min is inclusive and max is exclusive.
+
+```golang
+func getRandomInt(min, max int) int {
+ return rand.Intn(max-min) + min
+}
+
+// example
+for i := 0; i < 5; i += 1 {
+ fmt.Printf("%d ", getRandomInt(5, 10))
+}
+
+// output: 8 9 7 6 9
+```
+
+## Generate a random int between two values (inclusive)
+
+This example returns a random integer between min and max, where both min and max is inclusive.
+
+```golang
+func getRandomIntInclusive(min, max int) int {
+ return rand.Intn((max - min + 1)) + min
+}
+
+// example
+for i := 1; i <= 5; i += 1 {
+ fmt.Printf("%d ", getRandomIntInclusive(5, 10))
+}
+
+// output: 10 5 6 9 9
+```