lundi 27 juillet 2015

Random number generator output 0-9 [duplicate]

This question already has an answer here:

Hi i am having issues with my code at the moment as i have been stuck for a while now, i am new to java programming. I want the code output to be from 0-9 not 1-10, my code and output is displayed below:

import java.util.Arrays;

public class RandNumGenerator {

public static int RandInt(){
    double n = Math.random()*10;
    return (int) n;
    }

public static void randTest(int n){
    int [] counts = new int [10];
    int sampleSize = 10000;
    int RandNum = RandInt();

    System.out.println ("Sample Size: " + sampleSize);
    String[] intArray = new String[] {"Value","Count","Expected","Abs Diff","Percent Diff"};
    System.out.println(Arrays.toString(intArray));



    for(int i=0;i<n;i++){
        counts[ RandNum ] = counts[ RandNum ] + 1;
        System.out.println(counts[RandNum]);
        } 
    }

public static void main(String[] args) {
    randTest(10);
    }
}

output is 1-10, not 0-9:

Sample Size: 10000
[Value, Count, Expected, Abs Diff, Percent Diff]
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10




Aucun commentaire:

Enregistrer un commentaire