xcode 随机数生成器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7640783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:03:59  来源:igfitidea点击:

xcode random number generator

objective-cxcodefacebookrandom

提问by Harry Hall

i am building an app that shows a random facebook profile picture but do not know how to make the profile id a random number. I can obviously change it randomly in the code but need it to be random. It is the second NSString down that i need to be random. ANy help would be much appreciated!

我正在构建一个显示随机 Facebook 个人资料图片的应用程序,但不知道如何将个人资料 ID 设为随机数。我显然可以在代码中随机更改它,但需要它是随机的。这是我需要随机的第二个 NSString。任何帮助将非常感激!

- (IBAction) nextImagePush {
    NSString *prefix = @"http://graph.facebook.com/";
    NSString *profileId = @"517418970";
    NSString *suffix = @"/picture?type=large";
    NSString* url= [NSString stringWithFormat:@"%@%@%@", prefix, profileId, suffix];
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
    [imageView setImage:img];

回答by zaph

NSString *profileId = [NSString stringWithFormat:@"%0.10u", arc4random()];
NSLog(@"profileId: %@", profileId);

NSLog output:

NSLog 输出:

profileId: 1375609947

Updated to use arc4random_uniform. For an unbiased integer within a range:

更新为使用arc4random_uniform. 对于范围内的无偏整数:

+ (u_int32_t)randomInRangeLo:(u_int32_t)loBound toHi:(u_int32_t)hiBound {
    int32_t   range = hiBound - loBound + 1;
    u_int32_t random = arc4random_uniform(range);

    return loBound + random;
}

NSString *profileId = [NSString stringWithFormat:@"%0.9u", [AppDelegate randomInRangeLo:0 toHi:999999999]];
NSLog(@"profileId: %@", profileId);

NSLog output:

NSLog 输出:

profileId: 2500425665

Using SecRandomCopyBytes will require including the header file:

使用 SecRandomCopyBytes 将需要包含头文件:

#import <Security/SecRandom.h>

and the Framework:

和框架:

Security.framework.

Better yet for versions or OS X >= 10.7 or iOS >= 4.3 use arc4random_uniform

更好的版本或 OS X >= 10.7 或 iOS >= 4.3 使用 arc4random_uniform

+ (u_int32_t)randomInRangeLo:(u_int32_t)loBound toHi:(u_int32_t)hiBound
{
    int32_t   range = hiBound - loBound + 1;
    return loBound + arc4random_uniform(range);
}

回答by Adam Rosenfield

It's easy to pick a random integer with a function such as rand(3), random(3), or arc4random(3)and then use +[NSString stringWithFormat:]to turn that into a string using the %uformat specifier.

使用rand(3), random(3), orarc4random(3)等函数选择随机整数很容易,然后使用格式说明符+[NSString stringWithFormat:]将其转换为字符串%u

What's much harder is ensuring that the integer you pick corresponds to a validFacebook user ID:

更困难的是确保您选择的整数对应于有效的Facebook 用户 ID:

  • Does Facebook document anywhere what the possible valid IDs are?
  • Are they always within a certain range?
  • Are all IDs in that range valid?
  • Is there any way to query the range of valid IDs?
  • Facebook 是否在任何地方记录了可能的有效 ID?
  • 它们总是在某个范围内吗?
  • 该范围内的所有 ID 都有效吗?
  • 有没有办法查询有效ID的范围?

If the answer to all of those questions is "yes", then you can do it, otherwise it's going to be next to impossible unless Facebook provides an API for what you want, which I think is unlikely.

如果所有这些问题的答案都是“是”,那么您就可以做到,否则几乎不可能,除非 Facebook 为您提供所需的 API,我认为这不太可能。

回答by NightCoder

to generate random numbers in a fixed interval you can use arc4random()%x. x represents the maximum number you will want to obtain. Of course there are other ways but this one works great.

要以固定间隔生成随机数,您可以使用 arc4random()%x。x 表示您想要获得的最大数量。当然还有其他方法,但这个方法很好用。