Iphone Can';我不太懂随机化

Iphone Can';我不太懂随机化,iphone,objective-c,ios5,random,Iphone,Objective C,Ios5,Random,我已经建立了一个专门的卡应用程序。它所做的是允许用户“绘制”一张卡,然后查看该卡,并将其放回卡组中的随机位置。我唯一的问题是,卡片经常被放在卡片组的顶部 以下是.h文件的内容: @class _Card; @interface _Deck : NSObject @property (readonly) NSString *deckData; @property (readonly) NSUInteger count; @property (readonly) NSUInteger deckC

我已经建立了一个专门的卡应用程序。它所做的是允许用户“绘制”一张卡,然后查看该卡,并将其放回卡组中的随机位置。我唯一的问题是,卡片经常被放在卡片组的顶部

以下是.h文件的内容:

@class _Card;

@interface _Deck : NSObject

@property (readonly) NSString *deckData;
@property (readonly) NSUInteger count;
@property (readonly) NSUInteger deckCount;
@property (readonly) BOOL needsReset;
@property (readonly) NSArray *cards;

- (id)initWithArray:(NSArray *)array;
- (id)initWithContentsOfFile:(NSString *)filePath;

- (void)shuffle;
- (NSArray *)draw;
- (void)reset;
- (void)changeEdition;

@end
现在,这里是我的绘制方法,它将绘制一张卡(如果卡指定了多张卡),然后在允许的情况下将该卡放回卡组:

- (NSArray *)draw {

    // first, we create an array that can be returned, populated with the
    // cards that we drew
    NSMutableArray *drawArray = [[[NSMutableArray alloc] init] autorelease];

    // next, we get the top card, which is actually located in the 
    // indexArray (I use this for shuffling, pulling cards, etc.)
    NSNumber *index = [[[indexArray objectAtIndex:0] retain] autorelease];

    // now we get the card that the index corresponds to
    // from the cards array
    _Card *card = [cards objectAtIndex:[index integerValue]];

    // now I remove the index that we 
    // got from the indexArray...don't worry,
    // it might be put back in
    [indexArray removeObject:index];

    // if the card is supposed to discard after
    // draw, we leave it out
    if(!card.discard) {

        int insertIndex = arc4random_uniform(indexArray.count);

        // then I insert the card into the deck using the random, random 
        // number
        [indexArray insertObject:index atIndex:insertIndex];
    }

    _Card *cardCopy = [card copy];

    // we add the card to the array 
    // that we will return
    [drawArray addObject:cardCopy];

    // next, if the card is not the final card...
    if(!card.final) {

        // ...and the card has an 
        // additional draw specified...
        if(card.force) {

            // we keep drawing until we need to stop
            [drawArray addObjectsFromArray:[self draw]];
        }
    }

    return drawArray;
}

有什么我可能做错的吗?如果你需要更多的信息,请告诉我。提前感谢您提供的任何帮助。

如果我理解正确,问题是它正在indexArray的索引0处插入卡

好的,你试过这样的方法吗:

(暂时不要使用此行
[indexArray removeObject:index];

你的代码似乎还可以,我猜它只是不喜欢在。。。我添加日志只是为了让你看看它是否真的每次都在顶部插入日志。给我一个更新-如果这不起作用,我可以看看你的项目文件。

什么是“经常”呢

你在这里展示的看起来很完美

请记住,这是随机的,并且很可能(尽管很少)连续10次获得特定的数字从长远来看您应该得到一个均匀的分布

运行此例程10000000次左右,并检查每个数字的获得次数(确保每次牌组中的牌数相同),然后确定是否有问题


另外,您确定您的indexArray包含正确的值,并且没有在其中复制0吗?

嗯,我要做的是我有两个
NSMutableArray
对象。第一个(卡)按plist文件中指定的顺序保存所有_卡对象。第二个(indexArray)包含一组表示卡片“索引”编号的
NSNumber
对象。我使用Fisher-Yates算法将
indexArray
数组随机化。然后,我拉动最上面的
索引(模拟拉动最上面的卡)。然后我尝试将该索引随机放回
索引数组中。希望这有助于理解问题,我做了这件事,以及测试的建议,现在它似乎给出了一个非常好的随机卡片。仍然一遍又一遍地重复着同样的牌,但是以一种更容易接受的方式。ThanksI将尝试像那样长时间地运行该例程。我的脑海中确实出现了索引的重复,我确实确定我没有。感谢另一个好问题是“是否
indexArray
确实是一个包含内容的真实对象,即
indexArray.count
始终不返回0?”好的,我尝试在10000000次迭代中计算随机数(哇,这比我预期的要长,哈哈)。在查看日志(这也花了很长时间…)之后,我确实看到它是一个非常随机的数字列表,介于0和索引总数之间。另外,为了澄清,我在创建卡片数组的同时创建了indexArray数组,因此它们都返回相同数量的对象(除非卡片被丢弃),谢谢。
if(!card.discard)
{
    int insertIndex = arc4random_uniform(indexArray.count);
    id obj = [indexArray objectAtIndex:index];
    [indexArray removeObjectAtIndex:index];
    [indexArray insertObject:obj atIndex:insertIndex];
NSLog(@"insertIndex is %i and obj is %@", insertIndex, obj);
}
else
{
    [indexArray removeObjectAtIndex:index];
}