- Home
- Registration
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn PHP
- Learn C++
- Learn Visual Basic
- Web Site Tips
- Miscellaneous
- Java Code Examples
- Learn Java
- Java Random Generator
- Thread Applet
- Sort Algorithms
- Computer Game
- Poet Applet
- Java Properties
- Java, MS Access, MySQL
- Site map
- Advertise here
- LaptopForLess
- Geeks´ Stuff
- Nerds´ Stuff
- Remote Control Toys
Random Numbers Generator Java Code Example
![]()
![]()
![]()
![]()
![]()
![]()
Here you will learn how to create a Java Applet that generates random numbers.
Any Java Applet must include a class that extends Java Applet class. If an applet is interactive and supposed to respond to some user actions the applet must implement ActionListener interface.
Any Java Applet must have init method or function. Method and function are synonyms in computer programming or, to be more correct, I would say that function that belongs to a class is called method. In the init method we have to declare and set Applet variables and applet layout.
In our case, we use Border Layout. Border Layout divides Applet area on 5 regions. Hence, it has five layout properties: BorderLayout.NORTH (top), BorderLayout.SOUTH (bottom) BorderLayout.EAST (right), BorderLayout.WEST (left), BorderLayout.CENTER.
In our code we define a panel for the "Generate" button, adding the button to the panel and adding the panel to the bottom of our applet (BorderLayout.SOUTH).
In the init method we declare an array to store six lotto numbers that will be generated by our applet. Initially, all numbers set to 0.
In our example user clicks Generate button, this button has btnGen name.
To make the button able to respond to user click, we are adding to the button an Action Listener.
btnGen.addActionListener(this);
Later, we defining what action should be performed when user clicks the button. Public function "public void actionPerformed(ActionEvent ev)" is used to define the desired action: generate random numbers one by one using system time as a seed.
Main function is added to the Applet to be able execute it as Java Application. In command prompts you can type ‘java RandomGen’ and application will be started. Please read start Java tutorial for details
To view an Applet in a browser you have to add the applet tag:<applet code="RandomGen.class" width="300" height="250"></applet>
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class RandomGen extends Applet implements ActionListener
{
Button btnGen;
int array[];
Panel btnpanel;
Color charColor[];
public void init()
{
setBackground(Color.white);
btnGen = new Button("Generate");
btnGen.addActionListener(this);
setLayout(new BorderLayout());
btnpanel=new Panel();
btnpanel.add(btnGen);
add(btnpanel, BorderLayout.SOUTH);
//Array to store lucky numbers
array = new int[] {0,0,0,0,0,0};
//Color for each lucky number
charColor=new Color[] {Color.red, Color.orange, Color.green, Color.pink, Color.blue, Color.black };
}
public void paint(Graphics g)
{
int h = 30;
int w = 13;
//Fonts for title
Font font = new Font("Verdana", Font.BOLD , 15);
g.setFont(font);
g.setColor(Color.red);
g.drawString("Lotto Winning Numbers", 20, h);
//lines to draw borders
g.drawLine(3,3, 3, 200);
g.drawLine(3,3, 247, 3);
g.drawLine(247,3, 247, 200);
//Vertical distance from title to numbers
h= h + 100;
//Fonts for numbers
Font font1 = new Font("Verdana", Font.BOLD , 19);
g.setFont(font1);
//Drawing numbers
for(int n=0;n<6;n++)
{
g.setColor(charColor[n]);
Integer I = new Integer(array[n]);
g.drawString(I.toString(), w, h);
w = w + 40;
}
//Drawing bottom border
g.setColor(Color.red);
g.drawLine(3,200, 247, 200);
}
public void actionPerformed(ActionEvent ev)
{
int exist=0;
int n =1;
//initialize rnd with current system time
Random rnd = new Random(System.currentTimeMillis());
//generate the first number
int i = Math.abs(rnd.nextInt() % 56);
//If number is 0 keep generate numbers until it is not 0; there is no 0 in Lottery
while(i==0)
i = Math.abs(rnd.nextInt() % 56);
//add number to array
array[0]= i;
//execute loop to slow down system
for(int j=0;j<1000000; j++)
n = n +1/n;
//generate the second unique number
int a = Math.abs(rnd.nextInt() % 56);
//make sure the number is unique
while((a==0)||(a== array[0]))
a = Math.abs(rnd.nextInt() % 56);
//add number to array
array[1]= a;
//Keep system busy for time to pass. If not enough time passed since generating
//the first number, the second number will be the same.We want it different
for(int j=0;j<1000000; j++)
n = n +1/n;
//generate the 3rd unique number
int b = Math.abs(rnd.nextInt() % 56);
//make sure the number is unique
while((b==0)||(b== array[0])||(b==array[1]))
b = Math.abs(rnd.nextInt() % 56);
//add number to array
array[2]= b;
for(int j=0;j<1000000; j++)
n = n +1/n;
//generate the 4th unique number
int c = Math.abs(rnd.nextInt() % 56);
//make sure the number is unique
while((c==0)||(c== array[0])||(c==array[1])||(c==array[2]))
c = Math.abs(rnd.nextInt() % 56);
//add number to array
array[3]= c;
for(int j=0;j<1000000; j++)
n = n +1/n;
//generate the 5th unique number
int d = Math.abs(rnd.nextInt() % 56);
//make sure the number is unique
while( (d==0)||(d== array[0])||(d==array[1])||(d==array[2])||(d==array[3]))
d = Math.abs(rnd.nextInt() % 56);
//add number to array
array[4]= d;
for(int j=0;j<1000000; j++)
n = n +1/n;
//generate the 6th unique number
int e = Math.abs(rnd.nextInt() % 56);
//make sure the number is unique and not 0
while((e==0)||(e== array[0])||(e==array[1])||(e==array[2])||(e==array[3])||(e==array[4]))
e = Math.abs(rnd.nextInt() % 56);
//add number to array
array[5]= e;
repaint();
}
public static void main(String[] args)
{
RandomGen r = new RandomGen();
}
}
Play with the applet and have fun.
Did you find information useful? Send to your friend a link to this page
Please rate the tutorial
| Comments |
|---|
Web programming Tips