# HG changeset patch # User antonc27 # Date 1444850749 -7200 # Node ID 2e80c98618189699895ae665e5c5bef151132461 # Parent 9fd7b8d3cf4d0bd5d6767a0e02c884765923b1f6 - Better random number generation for front-end: random() replaced with arc4random_uniform() diff -r 9fd7b8d3cf4d -r 2e80c9861818 project_files/HedgewarsMobile/Classes/GameConfigViewController.m --- a/project_files/HedgewarsMobile/Classes/GameConfigViewController.m Wed Oct 14 02:28:32 2015 +0200 +++ b/project_files/HedgewarsMobile/Classes/GameConfigViewController.m Wed Oct 14 21:25:49 2015 +0200 @@ -232,7 +232,6 @@ -(void) loadNiceHogs { @autoreleasepool { - srand(time(NULL)); NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Hedgehog/Idle.png",GRAPHICS_DIRECTORY()]; UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:filePath]; [filePath release]; @@ -245,10 +244,10 @@ [self.imgContainer removeFromSuperview]; self.imgContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)]; - NSInteger numberOfHogs = 1 + random() % 20; + NSInteger numberOfHogs = 1 + arc4random_uniform(20); DLog(@"Drawing %ld nice hedgehogs", (long)numberOfHogs); for (int i = 0; i < numberOfHogs; i++) { - NSString *hat = [hatArray objectAtIndex:random()%numberOfHats]; + NSString *hat = [hatArray objectAtIndex:arc4random_uniform((int)numberOfHats)]; NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat]; UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile:hatFile]; @@ -269,9 +268,9 @@ hog.animationDuration = 3; [animation release]; - int x = 20*i+random()%128; + int x = 20*i+arc4random_uniform(128); if (x > 320 - 32) - x = i*random()%32; + x = i*arc4random_uniform(32); hog.frame = CGRectMake(x, 25, hog.frame.size.width, hog.frame.size.height); [self.imgContainer addSubview:hog]; [hog startAnimating]; diff -r 9fd7b8d3cf4d -r 2e80c9861818 project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m --- a/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m Wed Oct 14 02:28:32 2015 +0200 +++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m Wed Oct 14 21:25:49 2015 +0200 @@ -252,8 +252,6 @@ } +(void) startSimpleGame { - srand(time(0)); - // generate a seed CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid); @@ -263,7 +261,7 @@ // pick a random static map NSArray *listOfMaps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL]; - NSString *mapName = [listOfMaps objectAtIndex:random()%[listOfMaps count]]; + NSString *mapName = [listOfMaps objectAtIndex:arc4random_uniform((int)[listOfMaps count])]; NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg",MAPS_DIRECTORY(),mapName]; NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL]; [fileCfg release]; @@ -276,8 +274,8 @@ NSArray *colorArray = [HWUtils teamColors]; NSInteger firstColorIndex, secondColorIndex; do { - firstColorIndex = random()%[colorArray count]; - secondColorIndex = random()%[colorArray count]; + firstColorIndex = arc4random_uniform((int)[colorArray count]); + secondColorIndex = arc4random_uniform((int)[colorArray count]); } while (firstColorIndex == secondColorIndex); unsigned int firstColor = [[colorArray objectAtIndex:firstColorIndex] intValue]; unsigned int secondColor = [[colorArray objectAtIndex:secondColorIndex] intValue]; diff -r 9fd7b8d3cf4d -r 2e80c9861818 project_files/HedgewarsMobile/Classes/HWUtils.m --- a/project_files/HedgewarsMobile/Classes/HWUtils.m Wed Oct 14 02:28:32 2015 +0200 +++ b/project_files/HedgewarsMobile/Classes/HWUtils.m Wed Oct 14 21:25:49 2015 +0200 @@ -103,14 +103,13 @@ +(NSInteger) randomPort { // set a new feed only at initialization time and forbid connecting to the server port if (activePorts == nil) { - srandom(time(NULL)); activePorts = [[NSMutableArray arrayWithObject:[NSNumber numberWithInt:NETGAME_DEFAULT_PORT]] retain]; } // pick a random number from the free ports list NSInteger res = 0; do { - res = (random() % 64511) + 1024; + res = (arc4random_uniform(64511)) + 1024; } while ([activePorts containsObject:[NSNumber numberWithInteger:res]]); // add this number to the forbdding list diff -r 9fd7b8d3cf4d -r 2e80c9861818 project_files/HedgewarsMobile/Classes/LevelViewController.m --- a/project_files/HedgewarsMobile/Classes/LevelViewController.m Wed Oct 14 02:28:32 2015 +0200 +++ b/project_files/HedgewarsMobile/Classes/LevelViewController.m Wed Oct 14 21:25:49 2015 +0200 @@ -31,7 +31,6 @@ #pragma mark View lifecycle -(void) viewDidLoad { [super viewDidLoad]; - srandom(time(NULL)); NSArray *array = [[NSArray alloc] initWithObjects: NSLocalizedString(@"Brutal",@""), @@ -133,7 +132,7 @@ if (theSwitch.on) { numberOfSections = 2; [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade]; - level = 1 + (random() % ([levelArray count] - 1)); + level = 1 + arc4random_uniform((int)[levelArray count] - 1); } else { numberOfSections = 1; [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade]; diff -r 9fd7b8d3cf4d -r 2e80c9861818 project_files/HedgewarsMobile/Classes/MapConfigViewController.m --- a/project_files/HedgewarsMobile/Classes/MapConfigViewController.m Wed Oct 14 02:28:32 2015 +0200 +++ b/project_files/HedgewarsMobile/Classes/MapConfigViewController.m Wed Oct 14 21:25:49 2015 +0200 @@ -62,7 +62,7 @@ [seed release]; // perform as if user clicked on an entry - NSIndexPath *theIndex = [NSIndexPath indexPathForRow:(random()%[source count]) inSection:0]; + NSIndexPath *theIndex = [NSIndexPath indexPathForRow:arc4random_uniform((int)[source count]) inSection:0]; [self tableView:self.tableView didSelectRowAtIndexPath:theIndex]; if (IS_NOT_POWERFUL([HWUtils modelType]) == NO) [self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; @@ -403,7 +403,6 @@ -(void) viewDidLoad { [super viewDidLoad]; - srandom(time(NULL)); // initialize some "default" values self.slider.value = 0.05f; diff -r 9fd7b8d3cf4d -r 2e80c9861818 project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m --- a/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m Wed Oct 14 02:28:32 2015 +0200 +++ b/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m Wed Oct 14 21:25:49 2015 +0200 @@ -54,7 +54,7 @@ } -(void) viewWillAppear:(BOOL)animated { - NSIndexPath *indexPath = [NSIndexPath indexPathForRow:random()%[self.listOfMissions count] inSection:0]; + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arc4random_uniform((int)[self.listOfMissions count]) inSection:0]; [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; [self tableView:self.tableView didSelectRowAtIndexPath:indexPath]; [super viewWillAppear:animated]; diff -r 9fd7b8d3cf4d -r 2e80c9861818 project_files/HedgewarsMobile/Classes/VoicesViewController.m --- a/project_files/HedgewarsMobile/Classes/VoicesViewController.m Wed Oct 14 02:28:32 2015 +0200 +++ b/project_files/HedgewarsMobile/Classes/VoicesViewController.m Wed Oct 14 21:25:49 2015 +0200 @@ -32,7 +32,6 @@ #pragma mark View lifecycle -(void) viewDidLoad { [super viewDidLoad]; - srandom(time(NULL)); voiceBeingPlayed = NULL; @@ -129,7 +128,7 @@ NSString *voiceDir = [[NSString alloc] initWithFormat:@"%@/%@/",VOICES_DIRECTORY(),[voiceArray objectAtIndex:newRow]]; NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:voiceDir error:NULL]; - int index = random() % [array count]; + int index = arc4random_uniform((int)[array count]); voiceBeingPlayed = Mix_LoadWAV([[voiceDir stringByAppendingString:[array objectAtIndex:index]] UTF8String]); [voiceDir release];