65,202
社区成员




//随机生成10个8位二进制0 1 代码,并存到字符串数组里,比如使数组的第一个元素为随机生成的第一个代码串,第二个元素为随机生成的第二个代码串,以此类推。。。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i,j;
char bins[10][9];//二维数组来存放
srand(time(NULL));//种子,防止随机数据不变
for (i=0;i<10;i++) {
for (j=0;j<8;j++) {
bins[i][j]='0'+rand()%2;//放入随机数
}
bins[i][j]=0;//字符串数组,所以最后一位 '\0'
}
for (i=0;i<10;i++) {
printf("%s\n", bins[i]);//输出
}
return 0;
}
我想到的也是这样的,干净易懂利索#include<iostream>
#include<time.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<string>
using namespace std;
int a[9];
int main()
{
clock_t start,end;
start=clock();
string strTmp[25];
time_t t;
char s[32];
srand((unsigned) time(&t));
for(int i=0;i<16;i++)
{
for(int j=0; j<32; j++)
{
cout<< rand() % 2;
//将整型数字,转换成字符数组
sprintf(s,"%d",rand() % 2);
//将字符数组存入数组中
strTmp[i].append(s);
}
cout<<endl;
}
end=clock();
cout<<"Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"S"<<endl;
//打印字符数组
for(int i=0;i<16;i++)
{
cout<<strTmp[i]<<endl;
}
return 0;
}
//随机生成10个8位二进制0 1 代码,并存到字符串数组里,比如使数组的第一个元素为随机生成的第一个代码串,第二个元素为随机生成的第二个代码串,以此类推。。。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i,j;
char bins[10][9];
srand(time(NULL));
for (i=0;i<10;i++) {
for (j=0;j<8;j++) {
bins[i][j]='0'+rand()%2;
}
bins[i][j]=0;
}
for (i=0;i<10;i++) {
printf("%s\n", bins[i]);
}
return 0;
}
const int ChromLen = 8;
std::bitset<ChromLen> Chrom;
for(int j = 0; j != ChromLen; ++j)
{
Chrom[j] = (rand()%10 < 5) ? 0:1;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void ito2(char *dest, int num)
{
if (num == 0)
sprintf(dest, "%c", 0);
else
{
sprintf(dest, "%d", num % 2);
ito2(dest + 1, num / 2);
}
}
int main(void)
{
int num = 0, i;
char buff[12] = {0};
char *p[10];
srand(time(NULL));
for (;num < 10; num++)
{
i = rand() & 0xff;
ito2(buff, i);
p[num] = strdup(buff);
}
for (num = 0; num < 10; num++)
{
printf("%s\n", p[num]);
}
return 0;
}