随机二进制数生成器 [英] Random binary number generator

查看:1188
本文介绍了随机二进制数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我写这段代码用于生成一个随机二进制数,其中包含两个for循环,如下所示:



Hello
I wrote this piece of code for generating a random binary number with two for loop like the following:

<pre>srand(time(0));
	for (i = 1; i <= 11; i++)                                                               
	{                                                                                       
		buffer.push_back(i);
	}
	for (i = 1; i <= buffer.size(); i++)
	{
		buffer[i] = rand() % 2;
		std::cout << buffer[i] % 2 << " ";
	}
	std::cout << std::endl;







有2个问题:

1当我在我的整个代码中的适当位置替换它时,编译器(在visual studio 2017中)给我一个关于这一行的警告:srand(time(0));那个(警告C4244'参数':从'time_t'转换为'unsigned int',可能丢失数据)



2)我怎样才能减少for的数量循环只有一个for循环?



我尝试过:






there are 2 problems :
1) when I replace it in the proper place in my whole code, the compiler(in visual studio 2017) give me a warning about this line: srand(time(0)); that (Warning C4244 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data)

2)how I can decrease the number of for loop to have just one for loop?

What I have tried:

<pre lang="c++">
srand(time(0));
	for (i = 1; i <= 11; i++)                                                               
	{                                                                                       
		buffer.push_back(i);
	}
	for (i = 1; i <= buffer.size(); i++)
	{
		buffer[i] = rand() % 2;
		std::cout << buffer[i] % 2 << " ";
	}
	std::cout << std::endl;

推荐答案

类型转换警告是正常的,可以被抑制。你为什么不在一个循环中写它?

The type conversion warning is normal and can be supressed. Why dont you write it in one loop?
srand((unsigned int) time(0));

for (i = 1; i <= 11; i++)                                                               
{                                                                                       
	buffer.push_back(i);

	buffer[i] = rand() % 2;
	std::cout << buffer[i] << " ";// giving the result out ???
}


你为什么要取两次模数?实际上,为什么呢?通常使用rand的方式是为值定义范围并将其写为
Why are you taking the modulus of two twice? Actually, why do it at all? Usually the way rand is used is you define a range for your values and write it as
value = rand() % maxValue;

然后,值的范围为零到maxValue。在你试过的部分中,第一个循环仅用于初始化向量。这可以写成:

Then values will have a range of zero to maxValue. In the "what you have tried section", the first loop only serves the purpose of initializing the vector. This could be written as :

for( i = 0; i < valueCount; ++i )
{
    int value = rand() % maxValue;
    buffer.push_back( value );
}

您需要定义变量valueCount和maxValue以使其可用。 rand的返回范围为0到RAND_MAX。您可以使用reserve()将向量初始化为给定大小。如果你知道你将拥有多少,这可能是一个好主意。主要是大数字,所以没有重新分配。

You need to define the variables valueCount and maxValue to make this usable. The return of rand will range from 0 to RAND_MAX. You can initialize the vector to a given size with reserve(). If you know how many you are going to have that can be a good idea. Mostly for large numbers so there no reallocations.


这篇关于随机二进制数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆