简体   繁体   English

如何在 C++ 中生成随机二进制数?

[英]How to Generate Random Binary Numbers in C++?

I am trying to implement fuction to generate random binary numbers, using the following :我正在尝试使用以下方法实现函数来生成随机二进制数:

srand(time(NULL)); 
int S = rand() % 2;//continuous random variable between 0 and 1

and then :进而 :

 rand()%2; 

is continuously called 2^20 times ... however im not getting proper random numbers.. any suggestions for improvement ?被连续调用 2^20 次……但是我没有得到正确的随机数……有什么改进建议吗?

Is there a data type for binary numbers or eqv.?是否有二进制数或等式的数据类型? (instead of declaring an integer) (而不是声明一个整数)

Also, the only implementation of the seed of srand that i've seen is time(NULL) OR time(0) , is there a way to manipulate the seed to get better random numbers ?另外,我见过的 srand 种子的唯一实现是 time(NULL) OR time(0) ,有没有办法操纵种子以获得更好的随机数?

Binary numbers are just numbers in a different base.二进制数只是不同基数中的数字。 There is no issue if you just generate integers (base 10) and then, when outputting, convert them to binary.如果您只生成整数(基数为 10),然后在输出时将它们转换为二进制,则没有问题。

Check this question for more info on converting an integer into a "binary" string: C++ - Decimal to binary converting检查此问题以获取有关将整数转换为“二进制”字符串的更多信息: C++ - Decimal to binary conversion

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM