Actionscript 如何从单词词典中生成给定长度的随机文本行(装箱问题)?
我需要生成三行文本(基本上是jibberish),每行长度为60个字符,包括每行末尾的硬回车。这些行是从不同长度(通常为1-8个字符)的单词字典中生成的。任何单词不得使用超过一次,并且单词之间必须用空格分隔。我认为这本质上是一个箱子包装问题 到目前为止,我所采用的方法是创建一个单词的hashMap,根据单词的长度进行分组。然后我选择一个随机长度,从映射中提取一个该长度的单词,并将其附加到当前生成的行的末尾,考虑空格或硬返回。它有一半的时间是有效的,但另一半时间我陷入了无限循环,程序崩溃了 我遇到的一个问题是:当我向行中添加随机单词时,给定长度的单词组可能会耗尽。这是因为字典中每种长度的单词数量不一定相同,例如,可能只有一个单词的长度为1。所以,我可能需要一个给定长度的单词,但现在已经没有这个长度的单词了 下面是我到目前为止的总结。我在ActionScript工作,但希望能用任何语言深入了解这个问题。非常感谢Actionscript 如何从单词词典中生成给定长度的随机文本行(装箱问题)?,actionscript,random,bin-packing,Actionscript,Random,Bin Packing,我需要生成三行文本(基本上是jibberish),每行长度为60个字符,包括每行末尾的硬回车。这些行是从不同长度(通常为1-8个字符)的单词字典中生成的。任何单词不得使用超过一次,并且单词之间必须用空格分隔。我认为这本质上是一个箱子包装问题 到目前为止,我所采用的方法是创建一个单词的hashMap,根据单词的长度进行分组。然后我选择一个随机长度,从映射中提取一个该长度的单词,并将其附加到当前生成的行的末尾,考虑空格或硬返回。它有一半的时间是有效的,但另一半时间我陷入了无限循环,程序崩溃了 我遇到
dictionary // map of words with word lengths as keys and arrays of corresponding words as values
lengths // array of word lengths, sorted numerically
min = lengths[0] // minimum word length
max = lengths[lengths.length - 1] // maximum word length
line = ""
while ( line.length < 60 ) {
len = lengths[round( rand() * ( lengths.length - 1 ) )]
if ( dictionary[len] != null && dictionary[len].length > 0 ) {
diff = 60 - line.length // number of characters needed to complete the line
if ( line.length + len + 1 == 60 ) {
// this word will complete the line exactly
line += dictionary[len].splice(0, 1) + "\n"
}
else if ( min + max + 2 >= diff ) {
// find the two word lengths that will complete the line
// ==> this is where I'm having trouble
}
else if ( line.length + len + 1 < 60 - max ) {
// this word will fit safely, so just add it
line += dictionary[len].splice(0, 1) + " "
}
if ( dictionary[len].length == 0 ) {
// delete any empty arrays and update min and max lengths accordingly
dictionary[len] = null
delete dictionary[len]
i = lengths.indexOf( len )
if ( i >= 0 ) {
// words of this length have been depleted, so
// update lengths array to ensure that next random
// length is valid
lengths.splice( i, 1 )
}
if ( lengths.indexOf( min ) == -1 ) {
// update the min
min = lengths[0]
}
if ( lengths.indexOf( max ) == -1 ) {
// update the max
max = lengths[lengths.length - 1]
}
}
}
}
dictionary//以单词长度为键,以对应单词数组为值的单词映射
长度//字长数组,按数字排序
最小=长度[0]//最小字长
max=length[length.length-1]//最大字长
line=“”
而(直线长度<60){
len=长度[圆形(rand()*(length.length-1))]
if(dictionary[len]!=null&&dictionary[len]。长度>0){
diff=60-line.length//完成该行所需的字符数
if(line.length+len+1==60){
//这个单词将完全完成这一行
行+=字典[len]。拼接(0,1)+“\n”
}
否则如果(最小+最大+2>=差异){
//找出将完成该行的两个字长
//==>这就是我遇到麻烦的地方
}
否则如果(直线长度+长度+1<60-最大值){
//这个词很安全,所以只要加上就行了
行+=字典[len]。拼接(0,1)+“”
}
if(字典[len]。长度==0){
//删除所有空数组并相应地更新最小和最大长度
字典[len]=null
删除字典[len]
i=长度。索引of(len)
如果(i>=0){
//这么长的话已经用光了,所以
//更新长度数组以确保下一个
//长度是有效的
长度.接头(i,1)
}
if(长度索引of(最小)=-1){
//更新最小值
最小=长度[0]
}
if(长度索引of(最大)=-1){
//更新最大值
最大=长度[长度.长度-1]
}
}
}
}
dictionnary = Group your words by lengths (like you already do)
total_length = 0
phrase = ""
while (total_length < 60){
random_length = generate_random_number(1,8)
if (total_length + random_length > 60)
{
random_length = 60 - total_length // possibly - 1 if you cound \n and -2 if you
// append a blank anyway at the end
}
phrase += dictionnary.get_random_word_of_length(random_length) + " "
total_length += random_length + 1
}
dictionnary=Group
dictionnary = Group your words by lengths (like you already do)
total_length = 0
phrase = ""
while (total_length < 60){
random_length = generate_random_number(1,8)
if (total_length + random_length > 60)
{
random_length = 60 - total_length // possibly - 1 if you cound \n and -2 if you
// append a blank anyway at the end
}
phrase += dictionnary.get_random_word_of_length(random_length) + " "
total_length += random_length + 1
}