java 带种子的随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3583515/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:31:42  来源:igfitidea点击:

random number with seed

java

提问by Lalchand

Reference: link text

参考:链接文本

i cannot understand the following line , can anybody provide me some example for the below statement?

我无法理解以下行,有人可以为我提供以下语句的示例吗?

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers

如果使用相同的种子创建 Random 的两个实例,并且对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列

回答by Michael Borgwardt

Since you asked for an example:

既然你问了一个例子:

import java.util.Random;
public class RandomTest {
    public static void main(String[] s) {
        Random rnd1 = new Random(42);
        Random rnd2 = new Random(42);

        System.out.println(rnd1.nextInt(100)+" - "+rnd2.nextInt(100));
        System.out.println(rnd1.nextInt()+" - "+rnd2.nextInt());
        System.out.println(rnd1.nextDouble()+" - "+rnd2.nextDouble());
        System.out.println(rnd1.nextLong()+" - "+rnd2.nextLong());
    }
}

Both Randominstances will always have the same output, no matter how often you run it, no matter what platform or what Java version you use:

无论Random您运行它的频率如何,无论您使用什么平台或什么 Java 版本,这两个实例将始终具有相同的输出:

30 - 30
234785527 - 234785527
0.6832234717598454 - 0.6832234717598454
5694868678511409995 - 5694868678511409995

回答by Thorbj?rn Ravn Andersen

The random generator is deterministic. Given the same input to Random and the same usage of the methods in Random, the sequence of pseudo-random numbers returned to your program will be the same even in different runs on different machines.

随机生成器是确定性的。给定 Random 的相同输入以及 Random 中方法的相同用法,即使在不同机器上的不同运行中,返回给程序的伪随机数序列也将相同。

This is why it is pseudo-random - the numbers returned behavestatistically like random numbers except they can be reliably predicted. True random numbers are unpredictable.

这就是为什么它是伪随机-数字返回行为统计学像随机数字除非他们能够可靠地预测。真正的随机数是不可预测的。

回答by Buhake Sindi

The Random class basically is a Psuedorandom Number Generator(also known as Deterministic random bit generator) that generates a sequence of numbers that approximates the properties of random numbers. It's not generally random but deterministic as it can be determined by small random states in the generator (such as seed). Because of the deterministic nature, you can generate identical result if you the sequence of methods and seeds are identical on 2 generators.

Random 类基本上是一个随机数生成器(也称为确定性随机位生成器),它生成一个近似随机数属性的数字序列。它通常不是随机的,而是确定性的,因为它可以由生成器中的小随机状态(例如seed)确定。由于确定性,如果方法和种子的序列在 2 个生成器上相同,则可以生成相同的结果。

回答by Daniel Renshaw

The numbers are not really random, given the same starting conditions (the seed) and the same sequence of operations, the same sequence of numbers will be generated. This is why it would not be a good iea to use the basic Random class as part of any cryptograhic or security related code since it may be possible for an attacker to figure out which sequnce is being generated and predict future numbers.

数字并不是真正随机的,给定相同的起始条件(种子)和相同的操作序列,将生成相同的数字序列。这就是为什么将基本的 Random 类用作任何密码或安全相关代码的一部分并不是一个好的 iea 的原因,因为攻击者可能会弄清楚正在生成哪个序列并预测未来的数字。

For a random number generator that emits non-deterministic values, take a look at SecureRandom.

对于发出不确定值的随机数生成器,请查看SecureRandom

See Random number generation, Computational methodson wikipedia for more info.

有关更多信息请参阅维基百科上的随机数生成、计算方法

回答by Emile

This means that when you create the Random object (e.g. at the start of your program), you will probably want to start with a new seed. Mostly people choose some time related value, such as the number of ticks.

这意味着当您创建 Random 对象时(例如在程序开始时),您可能希望从一个新种子开始。大多数人选择一些与时间相关的值,例如刻度数。

The fact that the number sequences are the same given the same seed is actually very convenient if you want to debug your program: make sure you log the seed value and if something is wrong you can restart the program in the debugger using that same seed value. This means you can replay the scenario exactly. This would be impossible if you would (could) use a true random number generator.

如果您想调试程序,那么给定相同种子的数字序列相同的事实实际上非常方便:确保记录种子值,如果出现问题,您可以使用相同的种子值在调试器中重新启动程序. 这意味着您可以准确地重播场景。如果您(可以)使用真正的随机数生成器,这将是不可能的。

回答by nessence

With the same seed value, separate instances of Random will return/generate the same sequence of random numbers; more on this here: http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Tech/Chapter04/javaRandNums.html

使用相同的种子值,不同的 Random 实例将返回/生成相同的随机数序列;更多关于这里:http: //www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Tech/Chapter04/javaRandNums.html

Ruby Example:

红宝石示例:

class LCG; def initialize(seed=Time.now.to_i, a=2416, b=374441, m=1771075); @x, @a, @b, @m = seed % m, a, b, m; end; def next(); @x = (@a * @x + @b) % @m; end; end

irb(main):004:0> time = Time.now.to_i
=> 1282908389

irb(main):005:0> r = LCG.new(time)
=> #<LCG:0x0000010094f578 @x=650089, @a=2416, @b=374441, @m=1771075>
irb(main):006:0> r.next
=> 45940
irb(main):007:0> r.next
=> 1558831
irb(main):008:0> r.next
=> 1204687
irb(main):009:0> f = LCG.new(time)
=> #<LCG:0x0000010084cb28 @x=650089, @a=2416, @b=374441, @m=1771075>
irb(main):010:0> f.next
=> 45940
irb(main):011:0> f.next
=> 1558831
irb(main):012:0> f.next
=> 1204687

Based on the values a/b/m, the result will be the same for a given seed. This can be used to generate the same "random" number in two places and both sides can depend on getting the same result. This can be useful for encryption; although obviously, this algorithm isn't cryptographically secure.

根据 a/b/m 值,给定种子的结果将相同。这可用于在两个地方生成相同的“随机”数,并且双方都可以依赖于获得相同的结果。这对加密很有用;尽管很明显,该算法在密码学上并不安全。