码迷,mamicode.com
首页 > 其他好文 > 详细

产生随机数

时间:2014-10-10 17:11:14      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   for   strong   sp   div   

#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<iostream>
using namespace std;
#define random(x) (rand()%x)
//    printf("%d/n",random(100));
//rand()会返回一随机数值, 范围在0至RAND_MAX 间。
//RAND_MAX定义在stdlib.h, 其值为2147483647。
int main()
{
    int n;
    srand((int)time(0));
    FILE *fd;
    fd = fopen("data.in","wt");
    if(NULL == fd)
    {
        printf("open file error!/n");
        return 0;    
    }
    cin>>n;
    for(int x=0;x<n;x++)
    {
          fprintf(fd,"%d ",random(100));
    }   
    return 0;
}

产生n个随机数,n由键盘输入。

产生随机数

标签:style   blog   color   io   os   for   strong   sp   div   

原文地址:http://www.cnblogs.com/jianfengyun/p/4015813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!