C/C++/C#从数组中随机抽取一个数(要源码/重奖)

想从一个给定的(10,6)列数组中随机抽出数来,要简单,而且提供源码,奖分好说。

本人在Borland C++平台已经成功运行该代码,没问题的。
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int a[10][6]={1,2,3,4,5,6,7,8,9,0,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,
51,52,53,54,55,56,57,58,59,60};
srand(time(0));
cout<<a[rand()%10][rand()%6]<<endl;
cin.get();
return 0;
}

//从数组中提取一组(六个)随机数,
//一共循环抽取5次并且将抽取的结果
//保存在一个一个RADOM.TXT文件里
//(分行保存).
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<fstream>

using namespace std;

int main()
{
ofstream outClientFile("c:\\RADOM.TXT", ios::out);
if(!outClientFile)
{
cerr<<"File can not open"<<endl;
exit(1);
}
int a[10][6]={1,2,3,4,5,6,7,8,9,0,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,
51,52,53,54,55,56,57,58,59,60};
srand(time(0));
for(int j=0;j<5;j++)
{
for(int i=0;i<6;i++)
outClientFile<<setw(5)<<a[rand()%10][rand()%6];
outClientFile<<endl;
}
cin.get();
return 0;
}
/*保存后的格式为:右对齐5个字符位置
因为网页上不能显示空格,在此我举例说明
其中@表示空格,而非程序中显示
@@@@5@@@43@@@33@@@44@@@45@@@39
@@@@5@@@19@@@@4@@@21@@@51@@@@9
@@@13@@@28@@@18@@@51@@@49@@@49
@@@38@@@34@@@@6@@@32@@@15@@@@4
@@@31@@@17@@@39@@@16@@@@5@@@30
*/

//将给定数组从一个叫做SOURCE.TXT文件里
//读取10行6列的数组(文件里每行只有6个数)
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<fstream>

using namespace std;

void outputLine(int num1,int num2,int num3,int num4,int num5,int num6 )
{
cout<<setw(5)<<num1<<setw(5)<<num2<<setw(5)<<num3<<setw(5)<<num4<<setw(5)<<num5<<setw(5)<<num6<<endl;
}

int main()
{
ifstream inClientFile("c:\\SOURCE.TXT", ios::in);
if(!inClientFile)
{
cerr<<"File can not open"<<endl;
exit(1);
}
int num1,num2,num3,num4,num5,num6;
while(inClientFile>>num1>>num2>>num3>>num4>>num5>>num6)
outputLine(num1,num2,num3,num4,num5,num6);
cin.get();
return 0;
}
/*source文件格式和上面保存的格式是一样的
43 7 38 42 27 42
19 9 48 58 48 55
49 59 53 60 20 73
35 87 45 83 96 92
35 43 51 52 1 22
43 7 38 42 27 42
12 8 60 55 35 56
51 3 82 58 68 55
99 93 29 60 20 24
63 87 45 83 26 92
*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-09-22
假设数组名字为iDat[10][6];

初始化函数中:

srand(time(NULL));

随即抽取数据:

int x = rand()%10;
int y = rand()%6;

theData = iDat[x][y];

theData里面的值就是随即抽取的值了。
第2个回答  2007-09-24
#include<stdlib.h>
#include<stdio.h>

void main()
{
//二维数组的初始化
int b[10][6]=
{
{2,3,76,12,56,2},
{43,5,12,6,23,65},
{87,62,36,74,8,34},
{27,34,13,45,29,54},
{45,3,6,89,54,72},
{52,38,43,57,21,9},
{76,33,21,76,34,2},
{34,76,3,5,85,456},
{234,456,76,345,234,123},
{342,65,435,789,345,234}

};

printf("随即抽取的数为:%d\n",b[rand()%10][rand()%6]);
}
第3个回答  2007-09-22
以下代码为VC++6.0上编译运行通过.
如果要抽的是一组数可再联系.

#include"cstdlib"
#include"iostream"
#include"ctime"
using namespace std;

int a[10][6];

class chanshengshuzu //初始化10X6的数组
{
public:chanshengshuzu();
~chanshengshuzu();
};

class xuanze//显示随机取的数
{public:void xianshi();
};

int main()
{
srand((unsigned)time(NULL));
chanshengshuzu cssz();
xuanze pt;
pt.xianshi() ;
return 0;
}

chanshengshuzu::chanshengshuzu() {
extern a[10][6]={1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6,1,2,3,4,5,6};//此处可修改数组的值!
}

void xuanze :: xianshi ()
{ int x,y=0;
x=rand()%10;
y=rand()%6;
cout<<"这次随机抽取的是第"<<x<<"行,第"<<y<<"列的数: "<<a[x][y];
}
第4个回答  2007-09-22
rand只是伪随机。
相似回答