zoukankan      html  css  js  c++  java
  • 总结:求随机数的方法

    发现1:还是要把自己以前学习的知识系统地归纳到一个比较常常看到的地方,比如这里,然后再用到的时候找也比较容易找到。
    发现2:不要过于信赖自己的记忆能力,事实证明,除了特别特别重要的事情,记忆会在不经意见消失不见~~

    随机数的三种方法:
    1.    srand((unsigned)time(0));
            int i = rand() % 5;      

    2.    srandom(time(0));
            int i = random() % 5;

    3.    int i = arc4random() % 5;
           
      根据别人的总结:rand()实际并不是一个真正的伪随机数发生器,random()会相对好点,但也不算理想。
      arc4random() 是一个真正的伪随机算法,而且范围是rand()的两倍。

       关于:http://www.cnblogs.com/pinping/archive/2011/11/30/2268997.html


    4.求枚举下一个,如下:
    此方法是老大指点的,很实用。使用该方法,不论是添加类型或者是取消某个类型值,都很方便,且一目了然。

    typedef enum _CopyType
    {
        Copy0 = 0,
        Copy1 = 1,
        Copy2 = 2,
        Copy3 = 3    
    }CopyType;


    NSMutableArray *copyTypeList;
    int currentCopyIndex;
    -(id)init{
         currentCopyIndex = 0;
         copyTypeList = [NSMutableArray arrayWithObjects:
                                 [NSNumber numberWithInt:Copy0],
                                 [NSNumber numberWithInt:Copy1],
                                 [NSNumber numberWithInt:Copy2];
                                 nil];
         [copyTypeList reRandomOrder];//将可变数组随机排序

         /*如果最后一定要设定为某个类型的话,则可在数组的最后添加*/
         [copyTypeList addObject:[NSNumber numberWithInt:Copy3]];
    }

    //取得下一个CopyType
    -(CopyType)getNextCopyType 

         ++currentCopyIndex;
         if(currentCopyIndex < [copyTypeList count])
         {
               return [[copyTypeList objectAtIndex:currentCopyIndex] intValue];
         } 
    }

           

  • 相关阅读:
    PHP中feof()函数的猜测
    PHP curl函数模拟爬虫(操作cookie)
    django发音
    Nginx出现“413 Request Entity Too Large”错误解决方法
    Python开发利器WingIDE破解方法
    开启Apache mod_rewrite
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    python 正则学习
    php定时执行任务
    命令行CURL教程[转载]
  • 原文地址:https://www.cnblogs.com/pinping/p/2268999.html
Copyright © 2011-2022 走看看