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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Randomizer 
 

In this example we are discussing how to generate the random numbers by using the random() method of the Math class and change them into different random numbers by apply

 

Randomizer

                         

In this example we are discussing how to generate the random numbers by using the random() method of the Math class and change them into different random numbers by apply some mathematical operations onto them and display 10 random numbers on the console.

Description of program: 

In the given example first we are defining three class (by making them static) variables whose values can't be changed, then define a long type variable and also create a method Randomnumbergenerator(). After that we define a method randomFloat() that performs some calculation on that long type variable and returns a float value. Now we take randomInt() method and passes the max value upto which we want to generate the random numbers, this method rounds the float value returned by the randomFloat() method by using the round() method of the Math class. Now we generate a class TestRandom  to test the random number generated. In this class we create an object of the of the Randomizer class and print the values of the random numbers on the console.

 

public class Randomnumbergenrator {
  
  static final int first = 830844;
  static final int second = 39048;
  static final int third = 3840938;
  
  long randomnumber = 1;  
  
  public Randomnumbergenrator(long randomnumber) { 
    this.randomnumber = randomnumber;
    }
    
    public float randomFloat() {
      randomnumber = (randomnumber * second + third) % first;
      return (float)randomnumber/(float)first;
      }
      
      public int randomInt(int max) {
        return Math.round(max * randomFloat());
      }
}


Download of above program.

Here is the class TestRandom.java that test the random number generated.

 public class TestRandom {

    public static void main(String[] args) {

      System.out.println("Here are the numbers generated randomly upto 500");
      Randomizer random = new Randomizer(new java.util.Date().getTime());
      for(int i = 0; i < 10; i++)
      System.out.println(random.randomInt(500));
  }
 }

Here is the output:

C:\Examples>java TestRandom
Here are the numbers generated randomly upto 500
441
297
365
152
425
322
293
385
346
72

Download of this program.

 

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

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 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

[b]I've got an alternative and better way to do the same program:
/*Random Number generatior. Program made by - Swayam Narain
* This handy programs generates random numbers and prints them.
* Copyright
*/
public class random
{ public static void main() {
double k=0; int res=0;
for(int i=1;i<=15;i++)//15 random numbers
{
k=Math.random();
res=(int)((k*6)+1);//upto 6

System.out.println(i+": "+res);// prints the Random numbers.
}
}
}

Posted by Swayam Narain on Monday, 12.1.08 @ 02:13am | #82217

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.