[英]Java - How can I know which number would be generated by Math.rand() or Random?
Here is an algorithm problem that I am trying to solve(It's written in Korean.): https://www.acmicpc.net/problem/10944 这是我要解决的算法问题(用韩语编写): https : //www.acmicpc.net/problem/10944
If I simply translate the problem, I need to generate a number between 1 and 10,000. 如果仅翻译问题,则需要生成一个介于1到10,000之间的数字。 The resulting number must be the same as the random number which is generated by the server(a referee)
结果数必须与服务器(裁判员)生成的随机数相同
Is this possible? 这可能吗? Can I guess what the system server would generate and Can I match it with my program's result?
我可以猜测系统服务器将生成什么,并且可以将其与程序的结果相匹配吗?
I figured out that there are two kinds of way to make a random number in Java. 我发现用Java生成随机数有两种方法。
And I can manipulate(or predefine) the result with Random. 而且我可以使用Random来操纵(或预定义)结果。 And I just think it might be possible to solve the problem.
我只是认为有可能解决问题。
For example: 例如:
import java.io.IOException;
import java.util.Random;
public class Solution {
public static void main(String[] args) throws IOException {
Random rnd = new Random(1);
System.out.println(rnd.nextInt(10000));
rnd = new Random(1);
System.out.println(rnd.nextInt(10000));
}
}
It shows the same result because the parameter of Random is the same. 由于Random参数相同,因此显示相同的结果。 And I tried with Random.
我尝试使用Random。 But I couldn't solve that problem.
但是我无法解决这个问题。
Like this: 像这样:
import java.io.IOException;
import java.util.Random;
public class Solution {
public static void main(String[] args) throws IOException {
Random rnd = new Random(1);
System.out.println(rnd.nextInt(10000));
}
}
And as I remember, Math.random() is generated by system time. 而且我记得,Math.random()是由系统时间生成的。 But I can't find any information for that.
但是我找不到任何信息。 If that's true and it may be possible to solve the problem.
如果是这样,则有可能解决问题。 Because I can program with the system time and the result will always correspond.
因为我可以用系统时间进行编程,所以结果将始终对应。
Your question has the same basis as the google authenticator implementation. 您的问题与Google身份验证器实施的依据相同。 Most RSA tokens or 2factor authentications use this method to authenticate the user.
大多数RSA令牌或2factor身份验证都使用此方法对用户进行身份验证。 As mentioned in the comments use a common seed to solve the problem
如评论中所述,使用通用种子来解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.