可可触摸:rand() 返回相同的字符串

发布于 2024-08-20 04:20:23 字数 696 浏览 3 评论 0原文

这是我的代码:

-(void)randommoves
{

NSArray *possiblemoves =[NSArray arrayWithObjects:@"R ",@"R' ",@"L ",@"L' ",@"B ",@"B' ",@"F ",@"F' ",@"U ",@"U' ",@"D ",@"D' ", nil];
NSMutableString *finalmoves = [[NSMutableString alloc] init];
finalmoves = [NSMutableString stringWithCapacity:0]; 
[finalmoves retain];
int i = 0;
for (i=0; i<20; i++) {
    int r = rand() % 13;
    NSString *string = [possiblemoves objectAtIndex:r];
    [finalmoves appendString:string];
}
NSLog(@"%@",finalmoves);
[finalmoves release];
}

每次运行它时,我都会得到完全相同的字符串“D'B B'D L'D'F'L'B'U'DD D'L'URBF D'B'”

我想要它做什么每次运行它时都会给我一个新的移动集,

我已经运行了至少 30 次,以确保它不是侥幸,并且它确实返回了相同的字符串,而且确实如此。

Here's my code:

-(void)randommoves
{

NSArray *possiblemoves =[NSArray arrayWithObjects:@"R ",@"R' ",@"L ",@"L' ",@"B ",@"B' ",@"F ",@"F' ",@"U ",@"U' ",@"D ",@"D' ", nil];
NSMutableString *finalmoves = [[NSMutableString alloc] init];
finalmoves = [NSMutableString stringWithCapacity:0]; 
[finalmoves retain];
int i = 0;
for (i=0; i<20; i++) {
    int r = rand() % 13;
    NSString *string = [possiblemoves objectAtIndex:r];
    [finalmoves appendString:string];
}
NSLog(@"%@",finalmoves);
[finalmoves release];
}

And every time I run it I get the EXACT same string "D' B B' D L' D' F' L' B' U' D D D' L' U R B F D' B' "

What I want it to do is give me a new move set every time I run it

I've ran this at least 30 times to make sure that it wasn't a fluke, and that it really WAS returning the same string, and sure enough, it is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这样的小城市 2024-08-27 04:20:23

您需要确保首先为随机数生成器播种。

在进入循环之前执行以下操作:

srand(time(NULL));

you need to make sure you seed your random number generator first.

before entering your loop do:

srand(time(NULL));
错爱 2024-08-27 04:20:23

请注意,您创建了两次finalMoves。一次使用 [[NSMutableString alloc] init],然后再次使用 [NSMutableString stringWithCapacity:0]。这意味着您正在泄漏内存。

像这样清理这段代码怎么样:

static NSArray* sPossibleMoves = nil;

+ (void) initialize
{
    sPossibleMoves = [[NSArray arrayWithObjects: @"R ",@"R' ",@"L ",@"L' ",@"B ",@"B' ",@"F ",
        @"F' ",@"U ",@"U' ",@"D ",@"D' ", nil] retain];
}

- (void) randomMoves
{
    NSMutableString* finalmoves = [NSMutableString stringWithCapacity: 20];
    if (finalMoves != nil) {
        for (int i = 0; i < 20; i++) {
            [finalMoves appendString: [sPossibleMoves objectAtIndex:
                (rand() % [sPossibleMoves count])]];
        }
        NSLog(@"%@",finalmoves);
    }
}

假设你会经常调用它,那么将可能的移动保留在全局中是有意义的(因为 Objective-C 缺乏类变量)

Note that you are creating finalMoves twice. Once with [[NSMutableString alloc] init] and then again with [NSMutableString stringWithCapacity:0]. This means you are leaking memory.

How about cleaning up this code like this:

static NSArray* sPossibleMoves = nil;

+ (void) initialize
{
    sPossibleMoves = [[NSArray arrayWithObjects: @"R ",@"R' ",@"L ",@"L' ",@"B ",@"B' ",@"F ",
        @"F' ",@"U ",@"U' ",@"D ",@"D' ", nil] retain];
}

- (void) randomMoves
{
    NSMutableString* finalmoves = [NSMutableString stringWithCapacity: 20];
    if (finalMoves != nil) {
        for (int i = 0; i < 20; i++) {
            [finalMoves appendString: [sPossibleMoves objectAtIndex:
                (rand() % [sPossibleMoves count])]];
        }
        NSLog(@"%@",finalmoves);
    }
}

Assuming that you will call this often, it makes sense to keep the possible moves around in a global (because Objective-C lacks class variables)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文