Random class sample program in Java - nextGaussian(), nextBoolean(), nextBytes(), nextInt(), nextLong(), nextFloat(), nextDouble()
By: Manoj Kumar in Java Tutorials on 2007-09-14
The Random class is a generator of pseudorandom numbers. These are called pseudorandom numbers because they are simply uniformly distributed sequences. Random defines the following constructors:
Random()
Random(long seed)
The first version creates a number generator that uses the current time as the starting, or seed, value. The second form allows you to specify a seed value manually.
If you initialize a Random object with a seed, you define the starting point for the random sequence. If you use the same seed to initialize another Random object, you will extract the same random sequence. If you want to generate different sequences, specify different seed values. The easiest way to do this is to use the current time to seed a Random object. This approach reduces the possibility of getting repeated sequences.
There are six types of random numbers that you can extract from a Random object. Random Boolean values are available from nextBoolean(). Random bytes can be obtained by calling nextBytes(). Integers can be extracted via the nextInt() method. Long integers, uniformly distributed over their range, can be obtained with nextLong(). The nextFloat() and nextDouble() methods return a uniformly distributed float and double, respectively, between 0.0 and 1.0. Finally, nextGaussian() returns a double value centered at 0.0 with a standard deviation of 1.0. This is what is known as a bell curve.
Here is an example that demonstrates the sequence produced by nextGaussian(). It obtains 100 random Gaussian values and averages these values. The program also counts the number of values that fall within two standard deviations, plus or minus, using increments of 0.5 for each category. The result is graphically displayed sideways on the screen.
// Demonstrate random Gaussian values. import java.util.Random; class RandDemo { public static void main(String args[]) { Random r = new Random(); double val; double sum = 0; int bell[] = new int[10]; for (int i = 0; i < 100; i++) { val = r.nextGaussian(); sum += val; double t = -2; for (int x = 0; x < 10; x++, t += 0.5) if (val < t) { bell[x]++; break; } } System.out.println("Average of values: " + (sum / 100)); // display bell curve, sideways for (int i = 0; i < 10; i++) { for (int x = bell[i]; x > 0; x--) System.out.print("*"); System.out.println(); } } }
Here is a sample program run. As you can see, a bell-like distribution of numbers is obtained. (Your output may look a bit different since we are using Random.)
Average of values: 0.04895742052535768 *** **** ******** **************** ********************** ************** ************* ************ ****** *
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Comments