samedi 12 septembre 2020

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

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.

/* Lesson 8 Coding Activity Question 1 */

import java.util.Scanner;
import edhesive.testing.Math;

public class U2_L8_Activity_One{
  public static void main(String[] args){

    /* Write your code here */
  Scanner scan = new Scanner(System.in);
  
  System.out.println("Enter a positive integer.");
  int n = scan.nextInt();

  double ran = Math.random();
  int range = n;
  int min = 1;
  int answer = (int) (ran * range) + min;
  
  System.out.println("Printing 10 random integers from 1 to n...");
  System.out.println(answer);
  System.out.println(answer);
  System.out.println(answer);
  System.out.println(answer);
  System.out.println(answer);
  System.out.println(answer);
  System.out.println(answer);
  System.out.println(answer);
  System.out.println(answer);
  System.out.println(answer);
  }
}



Aucun commentaire:

Enregistrer un commentaire