I am trying to get only 20 random values out of 26 from the letters
that also have to include elements from the array name
. finalArray
would look like: ["S", "A", "M", "A", "N", "T", "H", "A", "I", "J", "K", "L", "S", "N", "O","P","Q", "R", "S", "A"]
(randomly)
我试图从26个字母中只取20个随机值,这些字母还必须包含数组名中的元素。finalArray样子:(“S”、“A”、“M”,“一个”,“N”,“T”,“H”,“一个”,“我”、“J”、“K”、“L”、“S”、“N”,“O”,P,Q,R,“S”,“A”)(随机)
So far:
到目前为止:
var letters: [String] = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O","P","Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
var name: [String] = ["S", "A", "M", "A", "N","T","H","A"]
//Create finalArray elements
var availableLetters = letters.filter { value in
!contains(name, value)
}
var finalArray = availableLetters + name
I tried to do:
我想做的事:
//get 20 objects
var length = name.utf16Count
var beforeArray = finalArray[0...19]
//minus length of the word
var letterCount = beforeArray.count - length
// add missing letters
beforeArray = letters[0...letterCount] + name
Which is obviously wrong and the results are very unstable. What could I use as a simple workaround? How could I implement it?
这显然是错误的,结果非常不稳定。我能用什么作为一个简单的解决方案呢?我如何实现它?
1 个解决方案
#1
2
It seems from your example that you want to take name
, and then right-pad it with random letters from the alphabet up to a length of 20. Since it looks like you don’t mind about repeating the random letters, this makes things a lot easier.
从你的例子中可以看出,你想要取名字,然后用从字母表中随机抽取的字母填充到20。因为看起来你不介意重复那些随机的字母,这让事情变得简单多了。
The tricky part is generating a sequence of n random numbers up to a maximum. If you have this, you can use these numbers as indices into your alphabet array to pick the random characters. Here’s one way to generate that sequence:
棘手的部分是生成n个最大随机数的序列。如果你有这个,你可以使用这些数字作为索引进入你的字母表数组来选择随机字符。这里有一种生成序列的方法:
// Struct representing a sequence of count
// random numbers in range 0..<max
struct RandomSequence: SequenceType {
let count: Int
let max: Int
func generate() -> GeneratorOf<Int> {
var i = 0
return GeneratorOf {
i++ < self.count
? Int(arc4random_uniform(UInt32(self.max)))
: nil
}
}
}
Once you have this, it can be used to generate the padding:
一旦你有了这个,它可以用来生成填充:
let name = Array("SAMANTHA")
let alphabet = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
let targetCount = 20
let paddingCount = targetCount - name.count
let ranseq = RandomSequence(count: paddingCount, max: alphabet.count)
let padding = map(ranseq) { alphabet[$0] }
let padded = name + padding
// padded = ["S", "A", "M", "A", "N", "T", "H", "A", "W", "O", "C", "M", "L", "B", "L", "A", "N", "H", "I", "I"]
If you actually want the name shuffled in with the random letters afterwards, look at the top answer to this question. But it’s probably easiest to do this as a second step to the above technique rather than try and combine both steps together.
如果你真的想用随机的字母来称呼这个名字,看看这个问题的答案。但这可能是最简单的方法,作为上述技术的第二步,而不是尝试将两个步骤结合在一起。
It’s worth noting that if name
is a string, and you want the result to end up as a string, you don’t need to put in an array for this approach to work:
值得注意的是,如果名称是字符串,而您希望结果以字符串形式结束,则不需要为这种方法添加数组:
let name = "SAMANTHA"
let alphabet = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
let targetCount = 20
let paddingCount = targetCount - count(name)
let ranseq = RandomSequence(count: paddingCount, max: alphabet.count)
let padding = map(ranseq) { alphabet[$0] }
let padded = name + padding
// padded = "SAMANTHASLHMPRDYRHFC"
#1
2
It seems from your example that you want to take name
, and then right-pad it with random letters from the alphabet up to a length of 20. Since it looks like you don’t mind about repeating the random letters, this makes things a lot easier.
从你的例子中可以看出,你想要取名字,然后用从字母表中随机抽取的字母填充到20。因为看起来你不介意重复那些随机的字母,这让事情变得简单多了。
The tricky part is generating a sequence of n random numbers up to a maximum. If you have this, you can use these numbers as indices into your alphabet array to pick the random characters. Here’s one way to generate that sequence:
棘手的部分是生成n个最大随机数的序列。如果你有这个,你可以使用这些数字作为索引进入你的字母表数组来选择随机字符。这里有一种生成序列的方法:
// Struct representing a sequence of count
// random numbers in range 0..<max
struct RandomSequence: SequenceType {
let count: Int
let max: Int
func generate() -> GeneratorOf<Int> {
var i = 0
return GeneratorOf {
i++ < self.count
? Int(arc4random_uniform(UInt32(self.max)))
: nil
}
}
}
Once you have this, it can be used to generate the padding:
一旦你有了这个,它可以用来生成填充:
let name = Array("SAMANTHA")
let alphabet = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
let targetCount = 20
let paddingCount = targetCount - name.count
let ranseq = RandomSequence(count: paddingCount, max: alphabet.count)
let padding = map(ranseq) { alphabet[$0] }
let padded = name + padding
// padded = ["S", "A", "M", "A", "N", "T", "H", "A", "W", "O", "C", "M", "L", "B", "L", "A", "N", "H", "I", "I"]
If you actually want the name shuffled in with the random letters afterwards, look at the top answer to this question. But it’s probably easiest to do this as a second step to the above technique rather than try and combine both steps together.
如果你真的想用随机的字母来称呼这个名字,看看这个问题的答案。但这可能是最简单的方法,作为上述技术的第二步,而不是尝试将两个步骤结合在一起。
It’s worth noting that if name
is a string, and you want the result to end up as a string, you don’t need to put in an array for this approach to work:
值得注意的是,如果名称是字符串,而您希望结果以字符串形式结束,则不需要为这种方法添加数组:
let name = "SAMANTHA"
let alphabet = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
let targetCount = 20
let paddingCount = targetCount - count(name)
let ranseq = RandomSequence(count: paddingCount, max: alphabet.count)
let padding = map(ranseq) { alphabet[$0] }
let padded = name + padding
// padded = "SAMANTHASLHMPRDYRHFC"