c語言生成隨機數並存入文件,c語言系統生成隨機數

本文目錄一覽:

用c語言產生隨機數如何將產生的隨機數保存下來

#include stdlib.h

#include stdio.h

#include time.h          

void main( void )

{

int i,x;

FILE*fp;

fp=fopen(“e:/data.txt”,”w”);  //保存到e盤data.txt中

srand( (unsigned)time( NULL ) );         

     for( i = 0; i 2000;i++ )                        

     {

         printf( ” %d\n”, x=rand() );

         fprintf(fp,”%d “,x);

     }

}

怎麼用C語言生成隨機數

假設要生成的隨機數是大於等於20且小於等於100的整數

#includestdio.h

#includetime.h

#includestdlib.h

void main() { int a,b,x;

  a=20; b=100;

  srand((unsigned int)time(NULL)); //用當前時間生成一個隨機數種子

  x=rand()%(b-a+1)+a;

  printf(“得到一個隨機數:%d\n”,x);

}

c語言中如何用隨機數產生一萬個數寫入文件中,排序後再寫入另一個文件中

隨機數

文件寫入

排序

這三個模塊即可

大體實現 如下

#include stdio.h

#include time.h

#include stdlib.h

#define N 10000

void sort(int *a)

{

}

main()

{

int a[N],i;

FILE *fp;

srand(time(NULL));

for(i=0;iN;i++)

a[i]=rand()%10000+1;//隨機數

sort(a);//排序

fp=fopen(“a.txt”,”w”);

for(i=0;iN;i++)

fprintf(fp,”%4d”,a[i]);

fclose(fp);

}

c語言 把得到的隨機數放入txt文件

請參考。文件操作部分有注釋……

//#include “stdafx.h”//vc++6.0加上這一行.

#include “stdio.h”

#include “string.h”

#include “time.h”

#include “stdlib.h”

void main(void){

FILE *fp;

char fname[30]=”test_”;

int i,M,x,y,k,*p,*ptmp;

printf(“Type i(1i32) m(1i = M)…\n”);

while(1){

scanf(“%d%d”,i,M);

if(1i = M) break;

printf(“Error! redo…\n”);

}

p=(int *)malloc(sizeof(int)*M);

ptmp=(int *)malloc(sizeof(int)*(y=1i));

if(p==NULL || ptmp==NULL){

printf(“Application memory failure…\n”);

exit(0);

}

srand((unsigned)time(NULL));

for(x=0;xy;ptmp[x]=1+x++);

for(x=0;xM;x++,y–){

p[x]=ptmp[k=rand()%y];

ptmp[k]=ptmp[y-1];

}

strcat(strcat(fname,itoa(i,(char *)ptmp,10)),”_”);//生成文件名.

strcat(strcat(fname,itoa(M,(char *)ptmp,10)),”.txt”);

free(ptmp);

for(x=0;xM;x++){//用選擇法排序.

for(k=x,y=k+1;yM;y++)

if(p[k]p[y]) k=y;

if(kx){

y=p[k];

p[k]=p[x];

p[x]=y;

}

}

if((fp=fopen(fname,”w”))==NULL){//創建文件.

printf(“Create file failure…\n”);

exit(0);

}

fprintf(fp,”%s %d\n%s %d\n”,”.i”,i,”.M”,M);//寫文件頭.

for(x=0;xM;fprintf(fp,”%d “,p[x++]));//將排序後的數據寫入文件.

free(p);//釋放工作空間.

fclose(fp);//關閉文件.

}

C程序 生成隨機數並存入.txt文件

你好,我是大魚兒不吐泡泡,用百度網盤分享給你,點開就可以保存,鏈接永久有效^_^鏈接: 

 提取碼: jbuj 複製這段內容後打開百度網盤手機App,操作更方便哦

–來自百度網盤超級會員v3的分享

如果有另外想要的資源或者鏈接失效,可以使用百度網盤推出的【收集文件】功能進行提問收集資源哦,無需轉存簡單快捷,功能鏈接: 微信小程序、百度網盤app、web端都已上線

原創文章,作者:XROA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/131923.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
XROA的頭像XROA
上一篇 2024-10-03 23:48
下一篇 2024-10-03 23:48

相關推薦

發表回復

登錄後才能評論