java - Testing a random number generator -


i having trouble printing out first 2 columns of results in table new programming having issues , wondering issue in code. brief states must create:

  • a parameterless static int method, randint(), return random integer in range 0..9 inclusive. method include call math.random().

  • a static void method named randtest takes single integer argument, n. should perform following actions:

  • declare int array of 10 elements named counts. used record how each possible value returned randint.

  • call randint n times, each time incrementing count of element of counts corresponding value returned.

  • print results console in clear tabular form. output should following:

enter image description here

this code:

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];          for(int i=0;i<n;i++){             counts[i] = randint();             system.out.println(counts[i]);             }          }      public static void main(string[] args) {         int samplesize = 1000;               system.out.println ("sample size: " + samplesize);         string[] intarray = new string[] {"value","count","expected","abs diff","percent diff"};         system.out.println(arrays.tostring(intarray));         randtest(10);         }     } 

public static void randtest(int n){

question think about: parameter here? hint: it's not 10... want n times?

counts[i] = randint();

you want create 10 random numbers , store them array? nope. want create "samplesize" numbers , increase array on correct position. correct position be?

counts[ correctposition ] = counts[ correctposition ] + 1;

...would more correct, if can figure out correctposition.

also move output main method randtest() have together.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -