// // CCRandom.m // CCFC // // Created by xichen on 11-12-23. // Copyright 2011 ccteam. All rights reserved. // #import "CCRandom.h" #import <stdlib.h> #import <mach/mach_time.h> #import "CCLog.h" @implementation CCRandom // it uses a slightly more compicated argorithm than common random + (long)generateRandomNum { static long randomValue = 0; srandom((unsigned)mach_absolute_time() + randomValue); randomValue = random(); return random(); } + (NSString *)generateRandomStringByLen:(int)len { char *str = (char *)malloc(len + 1); if(!str) return nil; memset(str, 0, len + 1); for(int i = 0; i < len; ++i) { str[i] = abs([self generateRandomNum] % 10) + '0'; LOG_CHAR(str[i]); } LOG_CSTR(str); NSString *ret = [NSString stringWithUTF8String:str]; free(str); return ret; } @end
可能有更新:
googlecode链接地址:http://code.google.com/p/iphone-common-codes-ccteam/source/browse/trunk/CCFC/files/CCRandom.m
github地址: https://github.com/cxsjabc/iphone-common-codes-ccteam/tree/master/CCFC/files/CCRandom.m
本文详细解读了CCRandom.m文件中实现的伪随机数生成算法,包括使用复杂算法生成随机数和字符串的方法,并提供了代码示例。重点介绍了如何在iOS应用中使用此代码进行随机数和字符串生成。
2292

被折叠的 条评论
为什么被折叠?



