产生特定区间随机数(整型和浮点型)的静态类方法

//!随机数的最大值
#define ARC4RANDOM_MAX      0x100000000

+ (NSInteger)createRandomsizeValueInt:(NSInteger)fromInt toInt:(NSInteger)toInt
{
    if (toInt < fromInt)
    {
        return toInt;
    }
    if (toInt == fromInt)  
    {
        return fromInt;
    }
    NSInteger randVal = arc4random() % (toInt - fromInt + 1) + fromInt;
    return randVal;
}

+ (double)createRandomsizeValueFloat:(double)fromFloat toFloat:(double)toFloat
{
    if (toFloat < fromFloat)
    {
        return toFloat;
    }
    if (toFloat == fromFloat)  
    {
        return fromFloat;
    }
    double randVal = ((double)arc4random() / ARC4RANDOM_MAX) * (toFloat - fromFloat) + fromFloat;
    return randVal;
}

posted @ 2012-03-15 21:02  周宏伟  阅读(456)  评论(0编辑  收藏  举报