数据类型的别名&&随机数_C

 

 

// Code file created by C Code Develop

#include "ccd.h"
#include "stdio.h"
#include "stdlib.h"
#include <time.h>
int main(int argc, char **argv)
{
    
    // 六、数据类型的别名
    /*使用typedef关键字给数据类型定义一个别名。特点:
    1)名称更短;
    2)更符合程序猿的习惯🍉
    */  
    
    //例如  unsigned int 起个别名 size_t    
    typedef unsigned int size_t;
    size_t  ii =  0;   // 等价于    int ii = 0 ; 
    printf("ii:%d\n\n", ii);
    
    //七、随机数
    /*在C语言中,我们使用 <stdlib.h> 头文件中的 srand和rand 函数来生成随机数。
    srand函数初始化随机数发生器(俗称种子),在实际开发中,我们可以用时间作为参数,只要每次播种的时间不同,那么生成的种子就不同,最终的随机数也就不同,通常我们采用 <time.h> 头文件中的 time 函数即可得到一个精确到秒的时间作为种子。
    *//*     
    void srand(unsigned int seed);   
    // 随机数生成器的初始化函数
    int  rand();                        
    // 获一个取随机数
    */
    srand(time(0));  // 播下随机种子
    for(ii = 0; ii < 5; ii++) {
        printf("%d\n", rand()); //获取随机数
    }     
    printf("\n");
    
    //2.生成一定范围随机数
    int a = rand()  % 50; //生成0~49的随机数
    a = rand()  % 50 + 100; //生成100~150的随机数     
        
    return 0;
}

 

posted @ 2021-09-26 21:35  def_Class  阅读(57)  评论(0编辑  收藏  举报