|
@@ -32,22 +32,16 @@ func RandNum() string { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func GenerateUniqueRandomNumbers(n int) string { |
|
|
func GenerateUniqueRandomNumbers(n int) string { |
|
|
rand.Seed(time.Now().UnixNano()) // 初始化随机种子 |
|
|
|
|
|
numbers := make([]int, n) // 创建一个切片来保存随机数 |
|
|
|
|
|
for i := range numbers { |
|
|
|
|
|
j := 0 |
|
|
|
|
|
for { |
|
|
|
|
|
b := rand.Intn(10) // 生成0-9之间的随机数 |
|
|
|
|
|
numbers[i] = b |
|
|
|
|
|
for _, num := range numbers[:i] { |
|
|
|
|
|
if num == b { |
|
|
|
|
|
j++ |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if j == 0 { |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
rand.Seed(time.Now().UnixNano() + rand.Int63()) // 初始化随机种子 |
|
|
|
|
|
|
|
|
|
|
|
numbers := make([]int, 0, n) // 创建一个切片来保存随机数,初始容量为n |
|
|
|
|
|
used := make(map[int]bool) // 跟踪已使用的数字 |
|
|
|
|
|
|
|
|
|
|
|
for len(numbers) < n { |
|
|
|
|
|
b := rand.Intn(10) // 生成0-9之间的随机数 |
|
|
|
|
|
if !used[b] { |
|
|
|
|
|
numbers = append(numbers, b) |
|
|
|
|
|
used[b] = true |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -55,5 +49,6 @@ func GenerateUniqueRandomNumbers(n int) string { |
|
|
for _, v := range numbers { |
|
|
for _, v := range numbers { |
|
|
numbersStr += strconv.Itoa(v) |
|
|
numbersStr += strconv.Itoa(v) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return numbersStr |
|
|
return numbersStr |
|
|
} |
|
|
} |