zoukankan      html  css  js  c++  java
  • VC下的C语言程序随机数的产生

    本文章适用于VC编译器,VC编译器里有个rand()函数,我们用它来实现取随机数。

    #include <stdio.h>

    #include<stdlib.h> //随机数的头文件

    int main()

    {

    int k;

    k=rand();

    printf("%d ",k);

    return 0;

    }

    rand()可以产生0~32767的随机数

    那么,怎么产生0~100的随机数呢?

    我们只需要写成

    k=rand()%100;

    就可以了

    如果要产生 X~Y范围内的随机数

    我们只需要写成

    k=rand()%(X-Y+1)+X;

    就可以了

     

    要产生100个随机数,我们可以用数组来实现

    #include <stdio.h>

    #include <stdlib.h>

    void main()

    {

    int k[100];

    int i;

    for(i=0;i<100;i++)

    {

    k=rand()%100;

    printf("%d ",k);

    }

    }

     


    #include <time.h> 
    #include <stdlib.h> 
    #include <stdio.h>   
    void main() 

     int i,j;
     srand((int)time(0));
     for(i=0;i<10;i++) 
     { 
      j=1+(int)(10.0*rand()/(RAND_MAX+1.0)); 
      printf("%d ",j); 
     }
    }


  • 相关阅读:
    Lucky Permutation Triple 构造
    HDU4283:You Are the One(区间DP)
    D. Match & Catch 后缀数组
    数学选讲 orz
    D
    一步一步线段树
    湖科大校赛第三题
    最大流部分
    bfs题目集锦
    hdu1429 bfs+状态压缩
  • 原文地址:https://www.cnblogs.com/lightmare/p/10398863.html
Copyright © 2011-2022 走看看