简体   繁体   English

如何在Swift 4中防止函数生成相同的随机数集

[英]How to prevent function from generating same sets of random number in Swift 4

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. 我开发了一些代码来生成1到49之间的六个数字组成的五个数组。我使用arc4random_uniform(49)函数尝试做到这一点。 However, when appendArrays() is producing same set of number for all five loops. 但是,当appendArrays()为所有五个循环生成相同的一组数字时。

 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]] [[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. 您的问题是您永远不会重新初始化win数组。 Once it has 6 numbers, it never changes so you just keep appending the same win array to your win2 array of arrays. 一旦有6数字,它就永远不会改变,因此您只需将相同的win数组追加到win2数组中即可。

Instead of passing win as an inout [Int] into your appendArrays method, create a new win array each time in your main loop. 而不是将win作为inout [Int]传递到您的appendArrays方法中, appendArrays每次在主循环中创建一个新的win数组。

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. 另外,不清楚为什么要传入win2 ,因为appendArrays无论如何appendArrays返回最终值。 You could just create win2 inside of the function. 您可以在函数内部创建win2

Also, you can sort the array once before adding it to win2 . 此外,您可以对数组进行一次排序,然后再将其添加到win2

If you want numbers in 1...49 then you need to add 1 to your arc4random_uniform(49) number. 如果要在1...49数字,则需要在arc4random_uniform(49)数字中添加1

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM