diff --git a/README.md b/README.md index ceb13c5..bdd6250 100644 --- a/README.md +++ b/README.md @@ -1 +1,29 @@ -# lottery-generator-python \ No newline at end of file +# 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.* diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9a69dd0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/tiennm99/go-lottery-generator + +go 1.21 diff --git a/main.go b/main.go new file mode 100644 index 0000000..14c9e37 --- /dev/null +++ b/main.go @@ -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 +} \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index 64f845f..0000000 --- a/main.py +++ /dev/null @@ -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") \ No newline at end of file