ios - 如何在 C4 中生成随机数? [C4框架]

标签 ios c4

如何使用 C4 Alpha Api 生成随机数?

最佳答案

在 C4 中,您可以使用 C4Math 类,它有一个名为 randomInt 的类级方法...

[C4Math randomInt:255]; 会给你一个 0 到 255 之间的随机数。

还有一个 randomIntBetweenA:andB: 方法......

[C4Math randomIntBetweenA:100 andB:200] 会给你一个介于 100 和 200 之间的 randomInt...

在幕后,这些方法看起来像这样:

+(NSInteger)randomInt:(NSInteger)value {
    srandomdev();
    return ((NSInteger)random())%value;
}

+(NSInteger)randomIntBetweenA:(NSInteger)a andB:(NSInteger)b{
    NSInteger returnVal;
    if (a == b) {
        returnVal = a;
    }
    else {
        NSInteger max = a > b ? a : b;
        NSInteger min = a < b ? a : b;
        NSAssert(max-min > 0, @"Your expression returned true for max-min <= 0 for some reason");
        srandomdev();
        returnVal = (((NSInteger)random())%(max-min) + min);
    }
    return returnVal;
}

关于ios - 如何在 C4 中生成随机数? [C4框架],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10390549/

相关文章:

ios - 在弧下的 iOS 5 中释放内存

ios - C4Label 截断显示的文字

ios - C4 创建时间戳

ios - 放大动画 - c4framework

ios - AutoLayout系统设置的NSAutoresizingMaskLayoutConstraint引入的神秘值

ios - 在 iOS 上访问或检查 Chrome 本地存储

ios - 获取屏幕上的所有触摸

iphone - 使用 FMDB 执行 sqlite 查询时遇到问题

iphone - 如何在 iphone 标签上显示两种颜色的百分比变化