简体   繁体   English

Java中C的stdlib rand()函数

[英]C's stdlib rand() function in Java

I've generated a series of random numbers from a known seed in C using srand() and rand() from stdlib. 我已经使用stdlib中的srand()rand()从C中的一个已知种子生成了一系列随机数。 I now need to generate the same series of numbers using the same seed from C in Java. 现在,我需要使用Java中C语言的相同种子来生成相同系列的数字。

Java's Random class documentation states it uses a "linear congruential formula". Java的Random类文档指出,它使用“线性同余公式”。 The documentation I've found on rand() says it uses a "linear congruential" generator, although I'm not sure if this is for one specific implementation. 我在rand()上找到的文档说它使用了“线性同余”生成器,尽管我不确定这是否用于一种特定的实现。

Does anyone know if both generators will produce the same numbers if given the same seed or if a port of srand() and rand() exists for Java? 有谁知道如果给定相同的种子,或者对于Java是否存在srand()rand()端口,两个生成器是否会产生相同的数字?

The C standard does not dictate the implementations of srand() and rand(). C标准不规定srand()和rand()的实现。 As such, different environments (OS, C libraries, architecture, etc.) will more than likely produce sequences of numbers that are different for the same seed value. 这样,不同的环境(OS,C库,体系结构等)很可能会产生相同种子值的不同数字序列。

Also, the implementation Java's Random class is not bound to any particular algorithm. 同样,实现Java的Random类也不绑定到任何特定算法。 Here again, different JVMs may very well produce different sequences for the same seed value. 同样,不同的JVM对于相同的种子值可能会产生不同的序列。 In addition, the implementation will more than likely not be tied to the standard C functions. 另外,该实现很可能不会与标准C函数绑定在一起。 Which means, the Java produced sequence will be different than a C sequence using the same seed. 这意味着,Java生成的序列将与使用相同种子的C序列不同。

If you truly need to generate random sequence in Java to match exactly that of the standard C functions, the best you could hope to do is replicate the sequence for a particular environment. 如果您确实需要在Java中生成随机序列以与标准C函数的序列完全匹配,那么您最好希望做的就是针对特定环境复制该序列。 This would require creating a JNI library to proxy calls to srand() and rand() or creating some other external process that makes the calls and is invoked from Java. 这将需要创建一个JNI库以代理对srand()和rand()的调用,或者创建一些进行该调用并从Java调用的其他外部进程。 Either way, that's a lot of complexity and more program maintenance. 无论哪种方式,这都是很多复杂性和更多程序维护。

If in fact all you need are random sequences that appear to be uniformly distributed, regardless of the exact values, then use Random as is. 如果实际上您需要的只是看起来均匀分布的随机序列,而不管其确切值如何,请照原样使用Random。 It is more than sufficient for most RNG needs. 它足以满足大多数RNG需求。

As said in other answer, the C standard doesn't even specify that rand() will return the same sequence across different C platforms (libraries), and likewise nothing in Java guarantees that it matches any given C (or other Java) implementation. 如在其他答案中所述,C标准甚至没有指定rand()将在不同的C平台(库)之间返回相同的序列,同样,Java中没有任何东西可以保证它与任何给定的C(或其他Java)实现相匹配。 You could use JNI to call the specific C implementation on that platform, but this would only guarantee the same sequence to be produced when both the C and Java programs are run on the same platform using the same C library. 您可以使用JNI在该平台上调用特定的C实现,但这只能保证当C和Java程序都在同一平台上使用相同的C库运行时,产生相同的序列。

If you wish to ensure the same sequence in all situations, you need to implement the same random number generator in both languages. 如果希望在所有情况下都确保相同的顺序,则需要使用两种语言实现相同的随机数生成器。 A simple example can be found in POSIX.1-2001, and is quoted on many man 3 rand pages: 可以在POSIX.1-2001中找到一个简单的示例,并在许多man 3 rand页上引用了该示例:

static unsigned long next = 1;

/* RAND_MAX assumed to be 32767 */
int myrand(void) {
    next = next * 1103515245 + 12345;
    return((unsigned)(next/65536) % 32768);
}

void mysrand(unsigned seed) {
    next = seed;
}

It is trivially portable to Java. 它可以轻松移植到Java。 The quality of randomness produced this generator is not very high, but it's not really guaranteed to be any better with rand() either, so you would need to implement something fancier if better randomness is required. 生成器产生的随机性不是很高,但是也不能保证使用rand()会更好,因此,如果需要更好的随机性,则需要实现一些奇特的东西。

In both C and Java, the same seed will generate the same random values. 在C和Java中,相同的种子将生成相同的随机值。 While the underlying mechanism might be different, this property is maintained in every programming language I know about. 尽管底层机制可能有所不同,但是我所知道的每种编程语言均会维护此属性。

C and Java will probably not generate the same set of random numbers given a seed. 给定种子,C和Java可能不会生成相同的随机数集。 The only property that is maintained is that given a seed, a programming language will generate the same random numbers. 唯一保留的属性是给定种子,编程语言将生成相同的随机数。

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

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