Swift 生成无重复变量的随机计数的随机整数数组

Swift 生成无重复变量的随机计数的随机整数数组,swift,xcode,random,probability,repeat,Swift,Xcode,Random,Probability,Repeat,代码首先生成0-8之间的随机数,并将其分配给var n。然后,第二个随机数生成器func循环n次,以生成n个介于0和10之间的整数,所有整数都具有不同的发生概率,并最终放入一个数组中。我想要的是,这10个可能的数字中没有一个可以重复,所以一旦选择了一个,它就不能再被其他n-1次运行func的人选择。我在想一个repeat while循环,或者if语句,或者一些涉及索引的东西,但我不知道具体是怎么做的,也不知道放在什么括号内。谢谢你的帮助!有人悄悄地说,这是地球上最具挑战性、最需要智慧的编码难题。

代码首先生成0-8之间的随机数,并将其分配给var n。然后,第二个随机数生成器func循环n次,以生成n个介于0和10之间的整数,所有整数都具有不同的发生概率,并最终放入一个数组中。我想要的是,这10个可能的数字中没有一个可以重复,所以一旦选择了一个,它就不能再被其他n-1次运行func的人选择。我在想一个repeat while循环,或者if语句,或者一些涉及索引的东西,但我不知道具体是怎么做的,也不知道放在什么括号内。谢谢你的帮助!有人悄悄地说,这是地球上最具挑战性、最需要智慧的编码难题。接受挑战

import UIKit

let n = Int(arc4random_uniform(8))

var a:Double = 0.2
var b:Double = 0.3
var c:Double = 0.2
var d:Double = 0.3
var e:Double = 0.2
var f:Double = 0.1
var g:Double = 0.2
var h:Double = 0.4
var i:Double = 0.2
var j:Double = 0.2
var k: [Int] = []

for _ in 0...n {
    func randomNumber(probabilities: [Double]) -> Int {
        let sum = probabilities.reduce(0, +)
        let rnd = sum * Double(arc4random_uniform(UInt32.max)) / Double(UInt32.max)
        var accum = 0.0
        for (i, p) in probabilities.enumerated() {
            accum += p
            if rnd < accum {
                return i
            }}
        return (probabilities.count - 1)
    }
    k.append(randomNumber(probabilities: [a, b, c, d, e, f, g, h, i, j]))
}
print(k)
导入UIKit
设n=Int(arc4random_-uniform(8))
变量a:Double=0.2
变量b:Double=0.3
变量c:Double=0.2
变量d:Double=0.3
变量e:Double=0.2
变量f:Double=0.1
变量g:Double=0.2
变量h:Double=0.4
变量i:Double=0.2
变量j:Double=0.2
变量k:[Int]=[]
对于0…n中的uu{
func随机数(概率:[Double])->Int{
设总和=概率。减少(0,+)
设rnd=sum*Double(arc4random_uniform(UInt32.max))/Double(UInt32.max)
var accum=0.0
对于概率中的(i,p)。枚举(){
累计+=p
如果rnd<累计{
返回i
}}
返回(probability.count-1)
}
k、 附加(随机数(概率:[a,b,c,d,e,f,g,h,i,j]))
}
印刷品(k)
伪代码-

1)generate a number between 1-8
    n
2)take empty array
    arr[]
3)loop from 0 to n
    1) generate a random no
        temp
    2) check if it is there in arr
            > if it is there in arr, generate another
    3) when you get a number which is not there in arr, insert it
这是一个python代码

import random

n = random.randint(1,8)
arr = []
print(n)
for each in range(n):
    temp = random.randint(1, 10)
    while temp in arr:
        temp = random.randint(1, 10)
        print(temp)
    arr.append(temp)
print(arr)
检查代码示例

伪代码-

1)generate a number between 1-8
    n
2)take empty array
    arr[]
3)loop from 0 to n
    1) generate a random no
        temp
    2) check if it is there in arr
            > if it is there in arr, generate another
    3) when you get a number which is not there in arr, insert it
这是一个python代码

import random

n = random.randint(1,8)
arr = []
print(n)
for each in range(n):
    temp = random.randint(1, 10)
    while temp in arr:
        temp = random.randint(1, 10)
        print(temp)
    arr.append(temp)
print(arr)

检查代码示例

SwiftAnkush答案的版本-

 let n = arc4random_uniform(7) + 1
 var arr: [UInt32] = []
 for _ in 0 ... n {
    var temp = arc4random_uniform(9) + 1
    while arr.contains(temp) {
        temp = arc4random_uniform(9) + 1
    }
    print(temp)
    arr.append(temp)
 }
print(arr)

希望这有帮助

Swift安库什答案的版本-

 let n = arc4random_uniform(7) + 1
 var arr: [UInt32] = []
 for _ in 0 ... n {
    var temp = arc4random_uniform(9) + 1
    while arr.contains(temp) {
        temp = arc4random_uniform(9) + 1
    }
    print(temp)
    arr.append(temp)
 }
print(arr)

希望这有帮助

我可以告诉你的psuedo代码,如果你想或一个适当的python代码是的,请!。。。。psuedo代码…您的示例有一个问题。您的十个数字(a到j)中只有四个值:0.1、0.2、0.3、0.4。选择这四个值后,第五个选择必须重复。在你的条件下不可能得到最多8个数字。不可能,因为这些值成为var发生的概率。当运行时,输出的是ints 0-10i可以告诉您psuedo代码(如果您需要)或正确的python代码(是的,请!)!。。。。psuedo代码…您的示例有一个问题。您的十个数字(a到j)中只有四个值:0.1、0.2、0.3、0.4。选择这四个值后,第五个选择必须重复。在你的条件下不可能得到最多8个数字。不可能,因为这些值成为var发生的概率。当运行时,输出的是ints 0-10它的功能!我可以问一下,为什么您使用arc4random_uniform(7)+1而不是(8)?因为arc4random_uniform(7)生成0到7之间的随机数,您能帮我吗。我不明白如何把这两种方法混为一谈。我给了我一个新的基于概率的数组,但是它们重复了,你给的那个不重复,但是不包含它所包含的概率!我可以问一下,为什么您使用arc4random_uniform(7)+1而不是(8)?因为arc4random_uniform(7)生成0到7之间的随机数,您能帮我吗。我不明白如何把这两种方法混为一谈。我给了我一个新的基于概率的数组,但它们重复了,你给的那个不重复,但不包含概率