- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to generate random numbers
The java.util.Random class instance is used to generate a stream of pseudorandom numbers.
- The class uses a 48-bit seed, which is modified using a linear congruential formula.
- The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudo randomly generated bits.
- The nextInt() method of this class is used to get the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
Example
import java.util.*; public class RandomNumberGeneration { public static void main( String args[] ) { // create random object Random randomno = new Random(); Scanner sc = new Scanner(System.in); System.out.println("How many random numbers do you need :"); int n= sc.nextInt(); // check next int value for(int i=0; i<n; i++){ System.out.println(randomno.nextInt()); } } }
Output
How many random numbers do you need : 4 709914869 -1882917719 -758130689 637944833
Advertisements