This is java program to generate the random numbers, using the Park-Miller algorithm.Park–Miller random number generator (after Stephen K. Park and Keith W. Miller), is a variant of linear congruential generator (LCG) that operates in multiplicative group of integers modulo n. A general formula of a random number generator (RNG) of this type is, x(n+1) = g*x(n) mod n.
Here is the source code of the Java Program to Implement Park-Miller Random Number Generation Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | //This is a sample program to random numbers using Park Miller Random Numbers algorithm public class Park_Miller_Random_Numbers { static final long m = 2147483647L; static final long a = 48271L; static final long q = 44488L; static final long r = 3399L; static long r_seed = 12345678L; public static double uniform () { long hi = r_seed / q; long lo = r_seed - q * hi; long t = a * lo - r * hi; if (t > 0 ) r_seed = t; else r_seed = t + m; return r_seed; } public static void main (String[] argv) { double [] A = new double [ 10 ]; for ( int i= 0 ; i< 5 ; i++) A[i] = uniform(); for ( int i= 0 ; i< 5 ; i++) System.out.print ( " " + A[i]); } } |
Output:
1 2 3 | $ javac Park_Miller_Random_Numbers.java $ java Park_Miller_Random_Numbers 1 .085252519E9 5 .08259731E8 1 .352291773E9 1 .563240271E9 8 .90733155E8 ... |
Related posts:
An Intro to Spring Cloud Vault
Java Program to Implement Bellman-Ford Algorithm
Java Program to Implement ArrayBlockingQueue API
Java Program to Implement Triply Linked List
Java IO vs NIO
Java Program to Find the Edge Connectivity of a Graph
Query Entities by Dates and Times with Spring Data JPA
Java Program to Implement Maximum Length Chain of Pairs
Guide to UUID in Java
Spring’s RequestBody and ResponseBody Annotations
Guide to CopyOnWriteArrayList
How to Get a Name of a Method Being Executed?
Using the Map.Entry Java Class
Java Program to Represent Linear Equations in Matrix Form
Spring Web Annotations
Anonymous Classes in Java
Performance Difference Between save() and saveAll() in Spring Data
Java Program to Implement Insertion Sort
Transactions with Spring and JPA
HTTP Authentification and CGI/Servlet
Validations for Enum Types
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Implement Stack
Spring Security Form Login
The HttpMediaTypeNotAcceptableException in Spring MVC
Guide To CompletableFuture
Java Convenience Factory Methods for Collections
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
REST Pagination in Spring
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Function trong Java 8
Java Program to Generate Random Numbers Using Multiply with Carry Method