ios 生成一个动态的随机的头像/随机数的操作

           

在写项目的时候,可能会遇到这种情况,用到集中随机的颜色,或者头像等,

首先:把所需要的图片放进一个数组当中

imgsAry = @[@"t1.png",@"t2.png",@"t3.png",@"t4.png",@"t5.png",@"t6.png",@"t7.png",@"t8.png"];

然后根据数组的count数生成一个随机数:

NSInteger randomIndex = arc4random()%imgsAry.count;

最后就可以直接展示了:             

 [self.headImageView sd_setImageWithURL:[NSURL URLWithString:self.userPhonto] placeholderImage:[UIImage imageNamed:[imgsAry                objectAtIndex:randomIndex]]];

随机颜色 oc:

#define GRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

swift:

let GRandomColor = UIColor(red: CGFloat(arc4random_uniform(256)/255), green: CGFloat(arc4random_uniform(256)/255), blue: CGFloat(arc4random_uniform(256)/255), alpha: 1)

随机数的生成和操作:

1、  获取一个随机整数范围在:[0,100)包括0,不包括100
int x = arc4random() % 100;
2、  获取一个随机数范围在:[500,1000],包括500,包括1000
int y = (arc4random() % 501) + 500;
3、  获取一个随机整数,范围在[from,to],包括from,包括to
-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to – from + 1)));
}
原文地址:https://www.cnblogs.com/hero11223/p/5395819.html