产生随机数

产生n个随机数,n由键盘输入。
#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((unsigned)time(NULL)); 
    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(3)+1);
    }   
    return 0;
}
 
原文地址:https://www.cnblogs.com/jianfengyun/p/4015813.html