I developed some code to generate five arrays of six numbers between 1 and 49. I used arc4random_uniform(49)
function to try to do just that. However, when appendArrays()
is producing same set of number for all five loops.
class six49: lottery {
var price: Int
var winner: [Int] = []
override init(draws: Int, lines: Int){
price = 3
super.init(draws: draws, lines: lines)
}
func appendArrays(win: inout [Int], win2: inout [[Int]]) -> [[Int]] {
while win2.count < lines {
while win.count < 6 {
let selection = arc4random_uniform(49)
if win.contains(Int(selection)){
continue
} else {
win.append(Int(selection))
win = win.sorted()
}
}
win2.append(win)
}
return win2
}
}
let result = six49(draws: 1, lines: 5)
var arr2: [[Int]] = []
var arr1: [Int] = []
var collection = result.appendArrays(win: &arr1,win2: &arr2)
print(collection)
Here is output:
[[11, 16, 27, 31, 37, 39], [11, 16, 27, 31, 37, 39], [11, 16, 27, 31, 37, 39], [11, 16, 27, 31, 37, 39], [11, 16, 27, 31, 37, 39]]
I have been looking everywhere to try to create different set of number every loop, but I can't seem to find any solution.
Your problem is that you never reinitialize the win
array. Once it has 6
numbers, it never changes so you just keep appending the same win
array to your win2
array of arrays.
Instead of passing win
as an inout [Int]
into your appendArrays
method, create a new win
array each time in your main loop.
func appendArrays(win2: inout [[Int]]) -> [[Int]] {
while win2.count < lines {
var win = [Int]()
while win.count < 6 {
let selection = arc4random_uniform(49)
if win.contains(Int(selection)){
continue
} else {
win.append(Int(selection))
win = win.sorted()
}
}
win2.append(win)
}
return win2
}
Also, it isn't clear why you're passing win2
in since appendArrays
returns the final value anyway. You could just create win2
inside of the function.
Also, you can sort the array once before adding it to win2
.
If you want numbers in 1...49
then you need to add 1
to your arc4random_uniform(49)
number.
func appendArrays() -> [[Int]] {
var win2 = [[Int]]()
while win2.count < lines {
var win = [Int]()
while win.count < 6 {
let selection = Int(arc4random_uniform(49) + 1)
if !win.contains(selection) {
win.append(selection)
}
}
win.sort()
win2.append(win)
}
return win2
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.