iOS随机数生成
来源:互联网 发布:高程测量软件 编辑:程序博客网 时间:2024/06/05 22:21
感觉这个比较好用一些,先写一下它怎么用吧
//代表是从0-count之间的随机数
int r = arc4random()%count
//代表从x---x+y之间的随机数
int r = arc4random() %x +y;
下面举个列子吧
NSArray *array = [[NSArrayalloc]initWithObjects:@"0",@"1 ",@"2",@"3",@"4",@"5",@"6",@"7",nil];
NSMutableArray *randomArray = [[NSMutableArrayalloc]init];
while ([randomArray count] < array.count) {
int r = arc4random() %array.count;
[randomArray addObject:[array objectAtIndex:r]];
}
NSLog(@"%@",randomArray);
输出结果是这样的
(
7,
4,
2,
2,
0,
6,
2,
4
)
可以看到每次取值都是随机数,所以取得这8次有时候会取到重复的值--------下面是取出不重复的随机数------
//随机数从这里边产生
NSMutableArray *startArray=[[NSMutableArrayalloc]initWithObjects:@0,@1,@2,@3,@4,@5,@6,@7,nil];
//随机数产生结果
NSMutableArray *resultArray=[NSMutableArrayarray];
//随机数个数
NSInteger m=8;
for (int i=0; i<m; i++) {
int t=arc4random()%startArray.count;
resultArray[i]=startArray[t];
//移除已经出现的数值
[startArray removeObjectAtIndex:t];
}
NSLog(@"%@",resultArray);
打印结果(
6,
7,
4,
0,
2,
3,
5,
1
)
// 去掉UItableview headerview黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat sectionHeaderHeight = 100;
if ( (scrollView.contentOffset.y <= sectionHeaderHeight) && (scrollView.contentOffset.y >= 0) )
{
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y,0, 0, 0);
}
else if (scrollView.contentOffset.y >= sectionHeaderHeight)
{
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0,0, 0);
}
}
- ios 中生成随机数
- ios生成随机数
- iOS生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- 【IOS基础知识】生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios生成随机数方法
- ios 中生成随机数
- ios 中生成随机数
- iOS随机数的生成
- ios 中生成随机数
- iOS生成随机数
- ios 生成随机数
- ios中生成随机数
- 第三章:CSS中的样式选择器
- 遍历Map
- spring-redis配置
- 关于16年2月14日以后上传AppStore出现:Missing iOS Distribution signing identity for...的问题
- hdu1166线段树模板求和
- iOS随机数生成
- json的使用
- java.io.FileNotFoundException-再次踩坑windows编码
- Android中退出多个Activity应用
- 你找bug我出钱
- org.json.JSONException: Unterminated object at character 97 of
- 【MySQL】性能优化之 覆盖索引
- C++ 容器及选用总结
- socket通信简介