如何将种子添加到伪数字发生器中?

发布于 2025-01-28 06:42:27 字数 600 浏览 2 评论 0原文

我目前正在处理一项连续创建三个头部的作业,但我让它工作了,但我未能分配给我的测试人员,因为它说我的随机数生成器需要为此特定问题采用种子参数。我的代码是:

public static void threeHeads() {
        Random r = new Random();
        //int seed = r.nextInt();
        int head =0;
        while (head!=3) {
            Boolean random = r.nextBoolean();
            
            if(random==false) {
                System.out.print("H");
                head++;
            }
            else {
                System.out.print("T");
                head=0;
            }
        }
        System.out.println("\nThree heads in a row!");
        
    }

I am currently working on an assignment with creating three heads in a row and I got it working but I am failing the tester assigned to me because it's saying my random number generator needs to take a seed parameter for this particular problem. My code for this is :

public static void threeHeads() {
        Random r = new Random();
        //int seed = r.nextInt();
        int head =0;
        while (head!=3) {
            Boolean random = r.nextBoolean();
            
            if(random==false) {
                System.out.print("H");
                head++;
            }
            else {
                System.out.print("T");
                head=0;
            }
        }
        System.out.println("\nThree heads in a row!");
        
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵性 2025-02-04 06:42:27

随机构造函数用一种方法过载:随机(长籽),它使您可以用预设种子实例化随机数生成器。

The random constructor is overloaded with a method: Random(long seed), which allows you to instantiate a random number generator with a preset seed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文