lundi 28 septembre 2015

How Do I get a Random Variable Based on Time in Java

I am trying to make a program that picks random numbers between 1 and 0. However, when I run the program, I get the same output over and over again (20 times!). How might I go about fixing this? I have heard that Math.random() is more biased than random().

This is my code:

public class Startup {

    public static void main(String[] args) {
        double match = Math.random();

        for(int i = 0; i <= 20; i++){
            if(match < 0.05){
                System.out.println("a");
            }
            else if(0.05 <= match && match <= 0.1){
                System.out.println("b");
            }
            else if(0.1 < match && match <= 0.15){
                System.out.println("c");
            }
            else if(0.15 < match && match <= 0.2){
                System.out.println("d");
            }
            else if(0.2 < match && match <= 0.25){
                System.out.println("e");
            }
            else if(0.25 < match && match <= 0.3){
                System.out.println("f");
            }
            else if(0.3 < match && match <= 0.35){
                System.out.println("g");
            }
            else if(0.35 < match && match <= 0.4){
                System.out.println("h");
            }
            else if(0.35 < match && match <= 0.4){
                System.out.println("i");
            }
            else if(0.4 < match && match <= 0.45){
                System.out.println("j");
            }
            else if(0.45 < match && match <= 0.5){
                System.out.println("k");
            }
            else if(0.5 < match && match <= 0.55){
                System.out.println("l");
            }
            else if(0.55 < match && match <= 0.6){
                System.out.println("m");
            }
            else if(0.6 < match && match <= 0.65){
                System.out.println("n");
            }
            else if(0.65 < match && match <= 0.7){
                System.out.println("o");
            }
            else if(0.7 < match && match <= 0.75){
                System.out.println("p");
            }
            else if(0.75 < match && match <= 0.8){
                System.out.println("q");
            }
            else if(0.8 < match && match <= 0.85){
                System.out.println("r");
            }
            else if(0.85 < match && match <= 0.9){
                System.out.println("s");
            }
            else if(0.9 < match && match <= 0.95){
                System.out.println("t");
            }
            else if(0.95 < match && match <= 1){
                System.out.println("u");
            }
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire