路过商场,看过抽奖感觉挺有意思的,商场进行抽奖活动,三个奖项,一等奖的概率1/10,二等奖的概率的3/10,三等奖的概率是6/10,具体奖品我没仔细看,回来随便练手了一下,思考了一下,奖品分为10份,生成一个随机数,如果0-5则是三等奖,6-8是二等奖,9是一等奖,简单实现如下:
int randomNumber=arc4random()%10; if (randomNumber>=0&&randomNumber<=5) { [self alert:@"恭喜你获得三等奖手机充值卡999元"]; }else if(randomNumber>=6&&randomNumber<=8){ [self alert:@"恭喜你获得二等奖iPad3"]; }else{ [self alert:@"恭喜你获得一等奖iPhone6"]; }
简单提示:
-(void)alert:(NSString *)message{ // http://www.cnblogs.com/xiaofeixiang UIAlertView *alterView=[[UIAlertView alloc]initWithTitle:@"抽奖结果" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alterView show]; }
效果如下: