mirror of
https://github.com/tiennm99/lottery-generator.git
synced 2026-10-11 03:13:41 +00:00
feat(go): rewrite in Go
This commit is contained in:
1 parent
f7432edaf3
commit
18f3534333
4 files changed
+95
-24
No files matched your search
@@ -1 +1,29 @@
|
|||||||
# lottery-generator-python
|
# go-lottery-generator
|
||||||
|
|
||||||
|
|
||||||
|
A lottery number generator written in Go that generates random sequences and tracks when any sequence appears multiple times.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Generates random sequences of 6 numbers from 1-45
|
||||||
|
- Tracks sequence frequency using a map
|
||||||
|
- Stops when any sequence appears 6 times
|
||||||
|
- Prints the generation count and matching sequence
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go run main.go
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example Output
|
||||||
|
|
||||||
|
```
|
||||||
|
1831593
|
||||||
|
[37 6 43 5 11 31]
|
||||||
|
Done
|
||||||
|
```
|
||||||
|
|
||||||
|
## Original Python Version
|
||||||
|
|
||||||
|
*In 2025, I rewrote this project using Go. The original Python version of this project can be found at [feature/python](https://github.com/tiennm99/go-lottery-generator/tree/feature/python) branch.*
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
n := 45
|
||||||
|
seqLength := 6
|
||||||
|
matchTimes := 6
|
||||||
|
maxGeneration := 1000000000000
|
||||||
|
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
|
sequenceCount := make(map[string]int)
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
for count < maxGeneration {
|
||||||
|
sequence := generateRandomSequence(n, seqLength)
|
||||||
|
seqKey := sequenceToString(sequence)
|
||||||
|
sequenceCount[seqKey]++
|
||||||
|
|
||||||
|
if sequenceCount[seqKey] == matchTimes {
|
||||||
|
fmt.Println(count)
|
||||||
|
fmt.Println(sequence)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Done")
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateRandomSequence(n, seqLength int) []int {
|
||||||
|
numbers := make([]int, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
numbers[i] = i + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
rand.Shuffle(len(numbers), func(i, j int) {
|
||||||
|
numbers[i], numbers[j] = numbers[j], numbers[i]
|
||||||
|
})
|
||||||
|
|
||||||
|
return numbers[:seqLength]
|
||||||
|
}
|
||||||
|
|
||||||
|
func sequenceToString(sequence []int) string {
|
||||||
|
sorted := make([]int, len(sequence))
|
||||||
|
copy(sorted, sequence)
|
||||||
|
sort.Ints(sorted)
|
||||||
|
|
||||||
|
result := ""
|
||||||
|
for i, num := range sorted {
|
||||||
|
if i > 0 {
|
||||||
|
result += ","
|
||||||
|
}
|
||||||
|
result += fmt.Sprintf("%d", num)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
import random
|
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
n = 45
|
|
||||||
seq_length = 6
|
|
||||||
match_times = 6
|
|
||||||
max_generation = 1000000000000
|
|
||||||
|
|
||||||
sequence_count = defaultdict(int)
|
|
||||||
count = 0
|
|
||||||
|
|
||||||
while count < max_generation:
|
|
||||||
sequence = random.sample(range(1, n + 1), seq_length)
|
|
||||||
sequence.sort()
|
|
||||||
seq = tuple(sequence)
|
|
||||||
sequence_count[seq] += 1
|
|
||||||
if sequence_count[seq] == match_times:
|
|
||||||
print(count)
|
|
||||||
print(seq)
|
|
||||||
break
|
|
||||||
count += 1
|
|
||||||
|
|
||||||
print("Done")
|
|
||||||
Reference in new issue
Block a user