Tuesday, March 3, 2015

Random numbers

Science is about measurements and understanding complex problems.  Understanding by inductive reasoning requires reliable data points that can be analyzed for a pattern to draw conclusion.

True random numbers are very hard to create, so we can test how Java creates random numbers and see if they are really random.

This is a sample code to get you started thinking about computer science as a scientist.  Run this code many times and chart the results to see the distribution of the numbers generated when you roll a dice in a Java application.  See if you can identify numbers that are more likely to show up than others and how consistently can you predict the most frequently occurring numbers.

import java.util.Scanner; // Needed for the Scanner class
import java.util.Random;

/**
 * Testing the random number generators in Java
 * @author Zoltan Szabo
 * @version 1.0.0
 */

public class RamdomTest
{
/**
 * Entry point to RamdomTest application
 * @param args as String array
 */
   public static void main(String[] args)
   {
      String name=new String();         // The user's name
      Random values=new Random(System.currentTimeMillis());  //Ramdom class allows to enter a seed value, you should try to set a single value vs. a dynamic value like the current time in this case
      int rand=abs(values.nextInt()%6);
      int x=0,zero=0, one=0,two=0,three=0,four=0,five=0;
      while(x++<1000000){
          switch(rand){
              case 0: zero++;break;
              case 1:one++;break;
              case 2:two++;break;
              case 3:three++;break;
              case 4:four++;break;
              case 5:five++;break;
            }
            rand=abs(values.nextInt()%6);
        }
     
     System.out.println(zero+"  " + one + "  " + two + "  " + three + "  " + four + "   " + five + " With current time as seed.  ") ;
 
     Random values2=new Random(5);  //constant seed value
     rand=abs(values2.nextInt()%6);
   
     x=0;
     x=0;zero=0; one=0;two=0;three=0;four=0;five=0;
           while(x++<1000000){
          switch(rand){
              case 0: zero++;break;
              case 1:one++;break;
              case 2:two++;break;
              case 3:three++;break;
              case 4:four++;break;
              case 5:five++;break;
            }
            rand=abs(values.nextInt()%6);
        }
     
     System.out.println(zero +"  " + one + "  " + two + "  " + three + "  " + four + "   " + five + " With constant value as seed.  " ) ;
 
     Random values3=new Random();  //no seed
     rand=abs(values3.nextInt()%6);
   
     x=0;
     x=0;zero=0; one=0;two=0;three=0;four=0;five=0;
           while(x++<1000000){
          switch(rand){
              case 0: zero++;break;
              case 1:one++;break;
              case 2:two++;break;
              case 3:three++;break;
              case 4:four++;break;
              case 5:five++;break;
            }
            rand=abs(values.nextInt()%6);
        }
     
     System.out.println(zero +"  " + one + "  " + two + "  " + three + "  " + four + "   " + five + " With no seed.  " ) ;
 
     double values_math;
     rand=(int)(Math.random()*10000)%6;
     x=0;
     x=0;zero=0; one=0;two=0;three=0;four=0;five=0;
       while(x++<1000000){
          switch(rand){
              case 0: zero++;break;
              case 1:one++;break;
              case 2:two++;break;
              case 3:three++;break;
              case 4:four++;break;
              case 5:five++;break;
            }
           rand=(int)(Math.random()*10000)%6;
        }
        System.out.println(zero +"  " + one + "  " + two + "  " + three + "  " + four + "   " + five + " With                   Math.random() method  ") ;
    }

private static int abs(int v){
    return (v<=0)?-v:v;
}
}
 

No comments:

Post a Comment