zoukankan      html  css  js  c++  java
  • iOS 随机数 不重复

    1、获取一个随机整数范围在:[0,100)包括0,不包括100
    
    int x = arc4random() % 100;
    
    2、  获取一个随机数范围在:[500,1000),包括500,不包括1000
    
    int y = (arc4random() % 501) + 500;
    
    
    
    /***********************/
    
    //生成0~9
        NSArray *temp = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6",@"7", @"8",@"9",nil];
        NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:temp];
        NSMutableArray *resultArray = [[NSMutableArray alloc] init];
        int i;
        int count = temp.count;
        NSLog(@"count:%d",count);
        for (i = 0; i < count; i ++) {
            int index = arc4random() % (count - i);
            [resultArray addObject:[tempArray objectAtIndex:index]];
            NSLog(@"index:%d,xx:%@",index,[tempArray objectAtIndex:index]);
            [tempArray removeObjectAtIndex:index];
            
        }
        [tempArray release];
        NSLog(@"resultArray is %@",resultArray);
    
    //输入出
        /*
        resultArray is (
                        9,
                        3,
                        1,
                        4,
                        5,
                        8,
                        7,
                        6,
                        2
                        )
    */
    
    //原理
    int index = arc4random() % (count - i); 
    //每取一个。减少一个。比如开始是从0~8 取一个后就少一个。
    [tempArray removeObjectAtIndex:index];
    //最重新的就是这句。它是把array 里的已取到的元素删掉,
  • 相关阅读:
    .gitignore 文件没起作用
    HTML 中img标签不显示
    关于拖拽
    关于javascript三目
    封装ajax
    javascript-时间戳
    关于Vue实例的生命周期created和mounted的区别
    ES6核心内容讲解
    jsonp跨域请求
    javascript-AJAX
  • 原文地址:https://www.cnblogs.com/qingjoin/p/2971121.html
Copyright © 2011-2022 走看看