如何从NSString正确随机化字母 [英] How to randomize letters correctly from an NSString

查看:158
本文介绍了如何从NSString正确随机化字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建单词加扰器,并且在随机化字母方面遇到问题.当字母随机化时,这是没有意义的.

I am creating a word scrambler and I am having issues randomizing the letters. When the letters get randomized, it doesn't make sense.

例如,单词PARK显示为AAPA.因此,正如您所知,到了该进行解读的时候,这对用户来说是没有意义的.

For example, the word PARK shows as AAPA. So, as you can tell it won't make sense for the user when it is time to unscramble.

请注意,我正在使用.plist文件保存单词.

Just so you know, I am using a .plist file to hold the words.

这是我用来随机化字母的代码:

This is the code I am using to randomize the letters:

    _words = [NSMutableArray arrayWithCapacity:scramblelength];

    for (int i=0;i<scramblelength;i++) { 

    NSString *letter = [scramble substringWithRange:[scramble rangeOfComposedCharacterSequenceAtIndex:arc4random()%[scramble length]]];

然后,我正在创建UIImageViews以显示加扰的单词:

Then, I am creating UIImageViews to display the scrambled words:

    if (![letter isEqualToString:@""]) {
        GameView *boxes = [[GameView alloc] initWithLetter:letter andSideLength:boxSide];
        boxes.center = CGPointMake(xOffset + i*(boxSide + kTileMargin), kScreenHeight/4*3);

        [self.scrambleView addSubview:boxes];
        [_words addObject:boxes];

我在这里做错了什么?我希望这些乱七八糟的单词中的字母有意义.

What am I doing wrong here? I would like for the letters in the scrambled words to make sense.

请帮助,我被困在这一个上了!

Please help, I am stuck on this one!

谢谢!

推荐答案

只要您的字符串长度适合32位,就可以了.如果没有,我将用C ++中的统一随机数生成器替换arc4random_uniform并将其编译为Objective-C ++模块.

As long as your string length will fit in 32 bits, this should be fine. If not, I would replace arc4random_uniform with a uniform random number generator in C++ and compile this as an Objective-C++ module.

代码只是简单地遍历字符串,并将每个组成字符序列与同一字符串中的一些随机组成字符序列交换.

The code simply iterates through the string, and swaps each composed character sequence with some random composed character sequence from the same string.

对不起,这是当您自大并且只输入代码时会发生的情况.让我知道您是否对此有疑问...

Sorry, that's what happens when you are arrogant and just type out code. Let me know if you have trouble with this one...

对于大得多的字符串,有一种更有效的方法,但这似乎可以解决问题.

For much larger strings, there is a more efficient way, but this seems to do the trick.

NSMutableString类别...

@interface NSMutableString (Scramble)
- (void)scramble;
@end

@implementation NSMutableString (Scramble)
static void
swapRanges(NSMutableString *string, NSRange iRange, NSRange jRange)
{
    // Need to replace the "trailing" component first
    if (NSEqualRanges(iRange, jRange)) return;
    if (iRange.location > jRange.location) {
        NSRange tmpRange = iRange;
        iRange = jRange;
        jRange = tmpRange;
    }
    NSString *iString = [self substringWithRange:iRange];
    NSString *jString = [self substringWithRange:jRange];
    [string replaceCharactersInRange:jRange withString:iString];
    [string replaceCharactersInRange:iRange withString:jString];
}

- (void)scramble
{
    for (NSUInteger i = 0; i < self.length; ++i) {
        NSRange iRange = [self rangeOfComposedCharacterSequenceAtIndex:i];
        NSUInteger j = arc4random_uniform(self.length);
        NSRange jRange = [self rangeOfComposedCharacterSequenceAtIndex:j];
        swapRanges(self, iRange, jRange);
    }
}
@end

NSString类别...

@interface NSString (Scramble)
- (NSString*)scrambledString;
@end
@implementation NSString (Scramble)
- (NSString *)scrambledString
{
    NSMutableString *result = [self mutableCopy];
    [result scramble];
    return [result copy];
}
@end

样品使用...

[someMutableString scramble];

NSString *mixedUp = [someString scrambledString];

或者,如果您熟悉C ++,则将其转换为std::wstring,调用std::random_shuffle,然后将其转换为NSString.使用经过验证的,经过良好测试的代码时,错误会少很多.

Or, if you are comfortable with C++, convert to a std::wstring, call std::random_shuffle, then convert that to a NSString. Lots less bugs when using proven, well tested code.

这篇关于如何从NSString正确随机化字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆