简体   繁体   English

Java-我怎么知道Math.rand()或Random将生成哪个数字?

[英]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生成随机数有两种方法。

  1. Random 随机
  2. Math.random() Math.random()

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.

相关问题 Java VS Matlab:Math.random()和rand - Java VS Matlab : Math.random() and rand 如何以随机方式擦洗 java 中的出生日期,这会导致当我喂食相同的原始出生日期时生成相同的随机数 - How to scrub date of birth in java in a random way which results in same random number generated when i feed the same original date of birth 如何在不使用Math.Random的情况下生成随机数? - How can I generate a random number without use of Math.Random? 我可以定义在math.random中显示特定数字的次数吗? (Java) - Can i define the amount of times a specific number is displayed in math.random? (Java) java Math类中的随机数 - Random number in java Math class java - 如何知道一个数字是否包含在另一个数字中? - How can I know if a number is contained in another in java? 我怎么知道创建了哪个类按钮生成了事件......? - How can i know the that in which class button is created which has generated event…? 如何将随机数加到Java(二十一点)的总数中? - How can i add the random number to my total for java(blackjack)? 数组列表中的Java jbutton,我怎么知道单击了哪个 - Java jbutton in an arraylist, how can i know which one is clicked 如何设置一个整数可以从一个随机数生成器中出现的最大时间? - How would I set the max amount of times an int can appear from a random number generator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM