Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
J2ME Random Number 
 

In this application we are going to generate the random number using Random class.

 

J2ME Random Number

                         

In this application we are going to generate the random number using Random class. The Random class within the java.util package and we can use it by importing the java.util.Random package. 

 

 

 

 

 

 

 

public void random(){
  Random number = new Random();
  float f = number.nextFloat();
  number.setSeed(System.currentTimeMillis());
  item.setText(""+(f*100.0f)%100);
}

In the above source code nextFloat returns the next pseudorandom, uniformly distributed float value from this random number generator's sequence and the setSeed method is used to sets the seed of this random number generator using a single long seed and finally by the using of canvas class we have to display the random number on the mobile window.

The Application is as follows:

 

RandomNumber.java 

 
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class RandomNumber extends MIDlet implements CommandListener{
  private Display display;
  private Command exit, generate;
  private StringItem item;
  private Form form;

  public RandomNumber(){
    display=Display.getDisplay(this);
    form = new Form("RandomNumber");
    exit = new Command("Exit", Command.EXIT,0);
    generate = new Command("Generate", Command.OK,1);    
    item = new StringItem("Number ","");
    form.addCommand(exit);
    form.addCommand(generate);
    form.setCommandListener(this);
    form.append("This Random Number Generated By Mr. Sandeep Kumar Suman,
    Software Developer, Roseindia Technology Pvt Ltd. Mobile No:+919313985248"
);
    form.append(item);
  }

  public void startApp(){
    display.setCurrent(form);
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){
    notifyDestroyed();
  }

  public void commandAction(Command c, Displayable d){
    String label = c.getLabel();
    if(label.equals("Exit")){
      destroyApp(false);
    }else if(label.equals("Generate")){
      random();
    }
  }

  public void random(){
    Random number = new Random();
    float f = number.nextFloat();
    number.setSeed(System.currentTimeMillis());    
    item.setText(""+(f*100.0f)%100);
  }
}

 

Download Source Code

                         

» View all related tutorials
Related Tags: c io vi lock this block set frame example ram exam locks e il manual blocks man in as m

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.