Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Generate random numbers

Wednesday, 3 October 2012

There are two means of generating random (really pseudo-random) numbers :
the Random class generates random integers, doubles, longs and so on, in various ranges.
the static method Math.random generates doubles between 0 (inclusive) and 1 (exclusive). To generate random integers :
do not use Math.random (it produces doubles, not integers)
use the Random class to generate random integers between 0 and N.

To generate a series of random numbers as a unit, you need to use a single Random object - do not create a new Random object for each new random number. See also the related Sun Tech Tip.

Another alternative is SecureRandom, a cryptographically strong subclass of Random. Example 1
 
import java.util.Random;

/** Generate 10 random integers in the range 0..99. */
public final class RandomInteger {
  
  public static final void main(String... aArgs){
    log("Generating 10 random integers in range 0..99.");
    
    //note a single Random object is reused here
    Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx){
      int randomInt = randomGenerator.nextInt(100);
      log("Generated : " + randomInt);
    }
    
    log("Done.");
  }
  
  private static void log(String aMessage){
    System.out.println(aMessage);
  }
} 

Example 2 
This example generates random integers in a specific range.  
import java.util.Random;

/** Generate random integers in a certain range. */
public final class RandomRange {
  
  public static final void main(String... aArgs){
    log("Generating random integers in the range 1..10.");
    
    int START = 1;
    int END = 10;
    Random random = new Random();
    for (int idx = 1; idx <= 10; ++idx){
      showRandomInteger(START, END, random);
    }
    
    log("Done.");
  }
  
  private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
    if ( aStart > aEnd ) {
      throw new IllegalArgumentException("Start cannot exceed End.");
    }
    //get the range, casting to long to avoid overflow problems
    long range = (long)aEnd - (long)aStart + 1;
    // compute a fraction of the range, 0 <= frac < range
    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber =  (int)(fraction + aStart);    
    log("Generated : " + randomNumber);
  }
  
  private static void log(String aMessage){
    System.out.println(aMessage);
  }
} 

Simple Java Program for beginners (HelloWorld.java)

Sunday, 30 September 2012


Writing a simple Hello World program is stepwise step. This short example shows how to write first java application and compile and run it. I am assuming that latest version of JDK is installed on your machine



HelloWorld.java - the source code for the "Hello, world!" program
 class HelloWorld { 
 public static void main(String[] args) 
 { 
 System.out.println("Hello World!"); 
 } 
} 
 
 


How to save
To run this program, save it in a file with the name HelloWorld.java. It must be sure that the file name must match the name of the class.

Compile the program

javac HelloWorld.java command is used to compile the source code.
When you compile the program you'll create a byte-code file named HelloWorld.class.
You can confirm this with the dir command in the DOS/Windows world.

Execute the byte code
Now you can execute the byte code in the Java interpreter with this command:
java HelloWorld
Output of the program
When you run the program at the command line, you'll see this output
Hello, world!
Related Posts Plugin for WordPress, Blogger...
 
Support : Copyright © 2011. GL Entertaintment - All Rights Reserved
Proudly powered by Blogger