如何随机生成8位二进制串并存到字符数组里

meilixinling888888 2013-04-23 10:45:50
如何随机生成多10个8位二级制0 1 代码,并存到字符串数组里,比如使数组的第一个元素为随机生成的第一个代码串,第二个元素为随机生成的第二个代码串,以此类推。。。
...全文
780 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
就是那个党伟 2013-05-03
  • 打赏
  • 举报
回复
//随机生成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;
}
我想到的也是这样的,干净易懂利索
chanya 2013-04-24
  • 打赏
  • 举报
回复
#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;
}
赵4老师 2013-04-24
  • 打赏
  • 举报
回复
//随机生成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;
}
feng038 2013-04-23
  • 打赏
  • 举报
回复

const int ChromLen = 8;		
std::bitset<ChromLen> Chrom;
for(int j = 0; j != ChromLen; ++j)
{
Chrom[j] = (rand()%10 < 5) ? 0:1;
}
Athenacle_ 2013-04-23
  • 打赏
  • 举报
回复

#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;
}


meilixinling888888 2013-04-23
  • 打赏
  • 举报
回复
#include<iostream.h> #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; srand((unsigned) time(&t)); for(int i=0;i<16;i++) { for(int j=0; j<32; j++) { cout<< rand() % 2; } cout<<endl; } end=clock(); cout<<"Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"S"<<endl; return 0; } 如何把这结果存到字符数组里啊

65,202

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧