C语言 如何生成大的随机数 C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7920860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate large random numbers C
提问by gfppaste
I'm looking for a way to generate large random numbers on the order of 2^64 in C... (100000000 - 999999999), to use in a public key encryption algorithm (as p and q).
我正在寻找一种在 C... (100000000 - 999999999) 中以 2^64 的顺序生成大随机数的方法,以用于公钥加密算法(如 p 和 q)。
I do not want to generate a number smaller than 2^64 (that is, smaller than 100000000).
我不想生成小于 2^64(即小于 100000000)的数字。
Is there anything that could help me to do this?
有什么可以帮助我做到这一点吗?
回答by David M. Syzdek
random() returns a long which on a 64bit system should be 64 bits. If you are on a 32bit system you could do the following:
random() 返回一个 long,在 64 位系统上应该是 64 位。如果您使用的是 32 位系统,您可以执行以下操作:
#include <inttypes.h>
uint64_t num;
/* add code to seed random number generator */
num = rand();
num = (num << 32) | rand();
// enforce limits of value between 100000000 and 999999999
num = (num % (999999999 - 100000000)) + 100000000;
Alternatively on a NIX system you could read /dev/random into your buffer:
或者,在 NIX 系统上,您可以将 /dev/random 读入缓冲区:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
int fd;
uint64_t num;
if ((fd = open("/dev/random", O_RDONLY) == -1)
{
/* handle error */
};
read(fd, &num, 8);
close(fd);
// enforce limits of value between 100000000 and 999999999
num = (num % (999999999 - 100000000)) + 100000000;
A
一种
回答by Blagovest Buyukliev
You could combine two 4-byte random integers to produce an 8-byte one:
您可以组合两个 4 字节的随机整数来生成一个 8 字节的随机整数:
#include <stdint.h>
...
uint64_t random =
(((uint64_t) rand() << 0) & 0x00000000FFFFFFFFull) |
(((uint64_t) rand() << 32) & 0xFFFFFFFF00000000ull);
Since randreturns int, and sizeof(int) >= 4on almost any modern platform, this code should work. I've added the << 0to make the intent more explicit.
由于randReturns int,并且sizeof(int) >= 4在几乎所有现代平台上,此代码应该可以工作。我添加了<< 0使意图更明确的。
The masking with 0x00000000FFFFFFFFand 0xFFFFFFFF00000000is to prevent overlapping of the bits in the two numbers in case sizeof(int) > 4.
用0x00000000FFFFFFFF和屏蔽0xFFFFFFFF00000000是为了防止两个数字中的位重叠以防万一sizeof(int) > 4。
EDIT
编辑
Since @Banthar commented that RAND_MAXis not necessarily 2 ^ 32, and I think it is guaranteed to be at least 2 ^ 16, you could combine four 2-byte numbers just to be sure:
由于@Banthar 评论说这RAND_MAX不一定是2 ^ 32,而且我认为它至少2 ^ 16可以保证是,因此您可以组合四个 2 字节数字以确保:
uint64_t random =
(((uint64_t) rand() << 0) & 0x000000000000FFFFull) |
(((uint64_t) rand() << 16) & 0x00000000FFFF0000ull) |
(((uint64_t) rand() << 32) & 0x0000FFFF00000000ull) |
(((uint64_t) rand() << 48) & 0xFFFF000000000000ull);
回答by wkl
You're looking for a cryptographic-strength PRNG, like openssl/rand: http://www.openssl.org/docs/crypto/rand.html
您正在寻找加密强度的 PRNG,例如openssl/rand:http: //www.openssl.org/docs/crypto/rand.html
回答by Basile Starynkevitch
You can make a large number Lout of smaller numbers (e.g. A& B). For instance, with something like L = (2^ n)*A + Bwhere ^ denotes exponentiation and nis some constant integer (e.g. 32). Then you code 1<<n(bitwise left-shift) for the power-of 2 operation.
您可以L从较小的数字(例如A& B)中生成一个较大的数字。例如, L = (2^ n)*A + B其中 ^ 表示求幂并且n是某个常量整数(例如 32)。然后1<<n为 2 的幂运算编码(按位左移)。
So you can make a large random number of of smaller random numbers.
因此,您可以制作大量随机数较小的随机数。
回答by MartyTPS
I know I'll probably get b____slapped by OliCharlesworth, but use rand() with a scale and offset. It's in stdlib.h In order to cover the whole range you should add that to another smaller rand() to fill in the gaps in the mapping.
我知道我可能会被 OliCharlesworth b____slapped,但使用带有比例和偏移量的 rand()。它在 stdlib.h 中 为了覆盖整个范围,您应该将其添加到另一个较小的 rand() 以填补映射中的空白。
回答by jwoods486
Or, you could use two random number generators with INDEPENDENT seeds and put their output numbers together as suggested. That depends whether you want a 64 bit number of a RNG with a period in the range of 2^64. Just don't use the default call that depends on the time, because you will get identical seeds for each generator. The right way, I just don't know ...
或者,您可以使用两个带有 INDEPENDENT 种子的随机数生成器,并按照建议将它们的输出数字放在一起。这取决于您是否想要一个周期在 2^64 范围内的 64 位 RNG。只是不要使用取决于时间的默认调用,因为每个生成器都会获得相同的种子。正确的方法,我只是不知道......

