如何让随机数在每次打印时都发生变化?

huangapple go评论126阅读模式
英文:

How can I make it so the random number is changed every time it prints?

问题

  1. /* Lesson 8 Coding Activity Question 1 */
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. public class U2_L8_Activity_One {
  5. public static void main(String[] args) {
  6. /* Write your code here */
  7. Scanner scan = new Scanner(System.in);
  8. System.out.println("Enter a positive integer.");
  9. int n = scan.nextInt();
  10. Random rand = new Random(); // Create a Random object
  11. System.out.println("Printing 10 random integers from 1 to n...");
  12. for (int i = 0; i < 10; i++) {
  13. int randomInt = rand.nextInt(n) + 1; // Generate a random integer between 1 and n
  14. System.out.println(randomInt);
  15. }
  16. }
  17. }
英文:

> Write the code to ask the user for a positive integer n, then print 10 random integers from 1 to n inclusive using Math.random().

How do I make it so that the output is different every time? As of now every output following the first one is exactly the same.

  1. /* Lesson 8 Coding Activity Question 1 */
  2. import java.util.Scanner;
  3. import edhesive.testing.Math;
  4. public class U2_L8_Activity_One{
  5. public static void main(String[] args){
  6. /* Write your code here */
  7. Scanner scan = new Scanner(System.in);
  8. System.out.println(&quot;Enter a positive integer.&quot;);
  9. int n = scan.nextInt();
  10. double ran = Math.random();
  11. int range = n;
  12. int min = 1;
  13. int answer = (int) (ran * range) + min;
  14. System.out.println(&quot;Printing 10 random integers from 1 to n...&quot;);
  15. System.out.println(answer);
  16. System.out.println(answer);
  17. System.out.println(answer);
  18. System.out.println(answer);
  19. System.out.println(answer);
  20. System.out.println(answer);
  21. System.out.println(answer);
  22. System.out.println(answer);
  23. System.out.println(answer);
  24. System.out.println(answer);
  25. }
  26. }

答案1

得分: 1

你已经正确地生成了一个随机数,但随后只是将其打印了十次。如果你想要十个不同的数,你需要每次重新生成一个新的随机数。例如:

  1. for (int i = 0; i < 10; ++i) {
  2. double ran = Math.random();
  3. int answer = (int) (ran * range) + min;
  4. System.out.println(answer);
  5. }
英文:

You properly generate a random number, but then just print it out ten times. If you want ten different numbers, you need to regenerate the number each time. E.g.:

  1. for (int i = 0; i &lt; 10; ++i) {
  2. double ran = Math.random();
  3. int answer = (int) (ran * range) + min;
  4. System.out.println(answer);
  5. }

答案2

得分: 1

你需要每次想要随机数时都调用Math.random()方法。你目前只调用了一次,并将结果赋值给了一个变量。调用System.out.println来打印这个变量不会改变结果,因为你打印的变量没有被重新赋值。

使用以下代码:

  1. System.out.println("随机数为:" + Math.random());
英文:

You need to call the Math.random() method every time you want a random number. You are calling it once and assigning the result to a variable. Calling a System.out.println to print the variable will not change the result, because there is no reassignment of the variable you're printing.

Go with

  1. System.out.println(&quot;The random number is &quot; + Math.random());

答案3

得分: 0

这应该可以工作:

  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. System.out.println("输入一个正整数。");
  4. int max = scan.nextInt();
  5. int min = 1;
  6. for (int i = 0; i < 10; ++i) {
  7. int ran = (int) (Math.random() * (max - min)) + min;
  8. System.out.println(ran);
  9. }
  10. }
英文:

This should work:

  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. System.out.println(&quot;Enter a positive integer.&quot;);
  4. int max = scan.nextInt();
  5. int min = 1;
  6. for (int i = 0; i &lt; 10; ++i) {
  7. int ran = (int) (Math.random() * (max - min)) + min;
  8. System.out.println(ran);
  9. }
  10. }

答案4

得分: -1

  1. import java.util.Scanner;
  2. import edhesive.testing.Math;
  3. public class U2_L8_Activity_One {
  4. public static void main(String[] args) {
  5. System.out.println("请输入一个整数");
  6. Scanner scan = new Scanner(System.in);
  7. int n = scan.nextInt();
  8. double ran = Math.random();
  9. int min = 1;
  10. int answer = (int) (Math.random() * n) + min;
  11. int answer1 = (int) (Math.random() * n) + min;
  12. int answer2 = (int) (Math.random() * n) + min;
  13. int answer3 = (int) (Math.random() * n) + min;
  14. int answer4 = (int) (Math.random() * n) + min;
  15. int answer5 = (int) (Math.random() * n) + min;
  16. int answer6 = (int) (Math.random() * n) + min;
  17. int answer7 = (int) (Math.random() * n) + min;
  18. int answer8 = (int) (Math.random() * n) + min;
  19. int answer9 = (int) (Math.random() * n) + min;
  20. System.out.println(answer);
  21. System.out.println(answer1);
  22. System.out.println(answer2);
  23. System.out.println(answer3);
  24. System.out.println(answer4);
  25. System.out.println(answer5);
  26. System.out.println(answer6);
  27. System.out.println(answer7);
  28. System.out.println(answer8);
  29. System.out.println(answer9);
  30. }
  31. }
英文:

This Will work when exciuted, and its done in a simple manner such as what you provided

  1. import java.util.Scanner;
  2. import edhesive.testing.Math;
  3. public class U2_L8_Activity_One{
  4. public static void main(String[] args){
  5. System.out.println(&quot;Please enter a integer&quot;);
  6. Scanner scan = new Scanner(System.in);
  7. int n = scan.nextInt();
  8. double ran = Math.random();
  9. int min = 1;
  10. int answer = (int) (Math.random() * n) + min;
  11. int answer1 = (int) (Math.random() * n) + min;
  12. int answer2 = (int) (Math.random() * n) + min;
  13. int answer3 = (int) (Math.random() * n) + min;
  14. int answer4 = (int) (Math.random() * n) + min;
  15. int answer5 = (int) (Math.random() * n) + min;
  16. int answer6 = (int) (Math.random() * n) + min;
  17. int answer7 = (int) (Math.random() * n) + min;
  18. int answer8 = (int) (Math.random() * n) + min;
  19. int answer9 = (int) (Math.random() * n) + min;
  20. System.out.println(answer);
  21. System.out.println(answer1);
  22. System.out.println(answer2);
  23. System.out.println(answer3);
  24. System.out.println(answer4);
  25. System.out.println(answer5);
  26. System.out.println(answer6);
  27. System.out.println(answer7);
  28. System.out.println(answer8);
  29. System.out.println(answer9);
  30. }
  31. }

huangapple
  • 本文由 发表于 2020年9月13日 10:32:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63866616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定