如何将种子添加到伪数字发生器中?
我目前正在处理一项连续创建三个头部的作业,但我让它工作了,但我未能分配给我的测试人员,因为它说我的随机数生成器需要为此特定问题采用种子参数。我的代码是:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
随机构造函数用一种方法过载:
随机(长籽)
,它使您可以用预设种子实例化随机数生成器。The random constructor is overloaded with a method:
Random(long seed)
, which allows you to instantiate a random number generator with a preset seed.