|
|
Program: Basic random number generator.
Description: |
Many times we need to generate sequence of numbers. It is not a problem to generate sequence of
numbers. But some times we need to generate random numbers. In java we can generate random numbers by using
Random class. By using Random class, we can generate random integers, long numbers and double values. Below example
shows how to generate random numbers using Random class.
|
Code: |
package com.java2novice.random;
import java.util.Random;
public class MyBasicRandom {
public static void main(String a[]){
Random rand = new Random();
System.out.println("Random Integers:");
System.out.println(rand.nextInt());
System.out.println(rand.nextInt());
System.out.println(rand.nextInt());
System.out.println("Random Double Numbers:");
System.out.println(rand.nextDouble());
System.out.println(rand.nextDouble());
System.out.println(rand.nextDouble());
System.out.println("Random Long Numbers:");
System.out.println(rand.nextLong());
System.out.println(rand.nextLong());
System.out.println(rand.nextLong());
}
}
|
|
Output: |
Random Integers:
1400247241
953993278
830962592
Random Double Numbers:
0.19841760470907022
0.32964328277263866
0.4624594748136943
Random Long Numbers:
4925512194471935414
2115815340178756456
4896741386551752249
|
|
|
|
|
List of Random class sample programs:- Basic random number generator.
- How to generate random numbers in the given range?
- How to generate same random sequence everytime?
- How to change Random class seed value?
- How to create random string with random characters?
|
|
|
String Vs StringBuffer
We know that String is immutable object. We can not change the value
of a String object once it is initiated. If we try to change the value of
the existing String object then it creates new object rather than changing
the value of the existing object. So incase, we are going to do more
modificatios on String, then use StringBuffer. StringBuffer updates the
existing objects value, rather creating new object.
Before you go and criticize the younger generation, just remember who raised them.
-- Unknown Author
|