Roll a six-sided die 6000 times : Random « Development « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Development » Random 
6. 37. 2. Roll a six-sided die 6000 times
import java.util.Random;

public class MainClass
{
   public static void mainString args[] )
   {
      Random randomNumbers = new Random()// random number generator

      int frequency1 = 0// count of 1s rolled
      int frequency2 = 0// count of 2s rolled
      int frequency3 = 0// count of 3s rolled
      int frequency4 = 0// count of 4s rolled
      int frequency5 = 0// count of 5s rolled
      int frequency6 = 0// count of 6s rolled

      int face; // stores most recently rolled value
   
      // summarize results of 6000 rolls of a die
      for int roll = 1; roll <= 6000; roll++ 
      {
         face = + randomNumbers.nextInt)// number from 1 to 6
   
         // determine roll value 1-6 and increment appropriate counter
         switch face 
         {   
            case 1:
               ++frequency1; // increment the 1s counter
               break
            case 2:
               ++frequency2; // increment the 2s counter
               break;
            case 3:
               ++frequency3; // increment the 3s counter
               break;
            case 4:
               ++frequency4; // increment the 4s counter
               break;
            case 5:
               ++frequency5; // increment the 5s counter
               break;
            case 6:
               ++frequency6; // increment the 6s counter
               break// optional at end of switch
         }
      }

      System.out.println"Face\tFrequency" )// output headers
      System.out.printf"1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n",
         frequency1, frequency2, frequency3, frequency4,
         frequency5, frequency6 );
   }
}
Face  Frequency
1 1005
2 1028
3 1027
4 989
5 994
6 957
6. 37. Random
6. 37. 1. Generating Random integer Numbers
6. 37. 2. Roll a six-sided die 6000 times
6. 37. 3. Random integers that range from from 0 to n
6. 37. 4. Random bytes
6. 37. 5. Random boolean
6. 37. 6. Random long type number
6. 37. 7. Random float type number
6. 37. 8. Random double type number
6. 37. 9. Create two random number generators with the same seed
6. 37. 10. Random number between 0 AND 10
6. 37. 11. Random numbers between 0.0 and 1.0
6. 37. 12. Random.nextInt(n) returns a distributed int value between 0 (inclusive) and n (exclusive).
6. 37. 13. Generate a random array of numbers
6. 37. 14. Random Gaussian values.
6. 37. 15. Operations for random Strings
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.