gpt4 book ai didi

objective-c - 为什么我总是得到相同的随机值?

转载 作者:太空宇宙 更新时间:2023-11-04 05:07:51 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
why do i always get the same sequence of random numbers with rand()?

我感到困惑的是,即使使用不同的程序(在同一台机器上)来运行/编译,并且在将函数的值(之前和之后)置零之后......不管是什么.. 我会不断得到相同的“随机”数字......每次我运行它。我发誓这不是它应该的工作方式。我将尽可能简单地说明......

#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {

int rPrimitive = 0; rPrimitive = 1 + rand() % 50;
NSNumber *rObject = nil; rObject = [NSNumber numberWithInt:rand() % 10];
NSLog(@"%i %@", rPrimitive, rObject);

rPrimitive = 0; rObject = nil;
NSLog(@"%i %@", rPrimitive, rObject);
return 0;
}

在 TextMate 中运行它:

i686-apple-darwin11-llvm-gcc-4.2
8 9
0 (null)

在 CodeRunner 中运行:

i686-apple-darwin11-llvm-gcc-4.2
8 9
0 (null)

运行它一百万次,如果你愿意的话。你可以猜到它永远是什么。为什么会这样?为什么哦,为什么会这样?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com