swift
1let Length = 10
2var integerArray = Array(repeating: 0, count: Length)
3for i in 0..<Length {
4 integerArray[i] = i+1
5}
6print(integerArray)
7//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
質問文中に「乱数」って書かれていないので。
真面目に
swift
1let Length = 10
2let array = [(Int, UInt32)](repeating: (0, 0), count: Length)
3
4//1~10に乱数で重みを付ける
5let weighted = array.enumerated().map {($0.0+1, arc4random_uniform(UInt32.max))}
6print(weighted)
7//=> [(1, 2394627293), (2, 3231339695), (3, 1429293380), (4, 2040053551), (5, 1055190483), (6, 2379829461), (7, 1949144040), (8, 522764666), (9, 1567954026), (10, 1966320976)]
8
9//重みでソートする
10let sorted = weighted.sorted {$0.1 > $1.1}
11print(sorted)
12//=> [(2, 3231339695), (1, 2394627293), (6, 2379829461), (4, 2040053551), (10, 1966320976), (7, 1949144040), (9, 1567954026), (3, 1429293380), (5, 1055190483), (8, 522764666)]
13
14//ソート結果から1~10を取り出す
15let integerArray = sorted.map {$0.0}
16print(integerArray)
17//=> [2, 1, 6, 4, 10, 7, 9, 3, 5, 8]
まとめると、
swift
1let integerArray = [(Int, UInt32)](repeating: (0, 0), count: 10)
2 .enumerated().map {($0.0+1, arc4random_uniform(UInt32.max))}
3 .sorted {$0.1 > $1.1}
4 .map {$0.0}
5print(integerArray)
6//=> [6, 3, 9, 8, 5, 10, 7, 1, 2, 4]