This is the java implementation of Multiply With Carry (MWC) algorithm to generate a set of random numbers. The main advantages of the MWC method are that it invokes simple computer integer arithmetic and leads to very fast generation of sequences of random numbers with immense periods. The formula it uses is, x(n) = (a*x(n-r) + c(n-1))mod n and c(n) = (a*x(n-r) + c(n-1))/n, where a is any multiplier, m is modulus, c is carry and r is initial number of seeds.
Here is the source code of the Java Program to Generate Random Numbers Using Multiply with Carry Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a sample program to generate a random numbers based on Multiply with carry method import java.util.Random; public class Multiply_With_Carry { public static void main(String args[]) { int max_Sequence_Elements = 10; Random random = new Random(); int base_b = 2000; int multiplier_a = random.nextInt(base_b); int r = 1; int []c = new int[max_Sequence_Elements]; int []x = new int[max_Sequence_Elements]; c[0] = random.nextInt(multiplier_a); x[0] = random.nextInt(base_b); System.out.print("The random number sequence is: " + x[0]); //generating sequence for(int i=1; i<max_Sequence_Elements; i++) { x[i] = (multiplier_a*x[i-r] + c[i-1]) % base_b; c[i] = (multiplier_a*x[i-r] + c[i-1]) / base_b; System.out.print(" " + x[i]); } } }
Output:
$ javac Multiply_With_Carry.java $ java Multiply_With_Carry The random number sequence is: 795 382 1487 260 475 1798 1347 722 1389 63
Related posts:
The Java 8 Stream API Tutorial
Java Program to Implement RoleUnresolvedList API
Spring Boot Security Auto-Configuration
HashSet trong java
Java Program to Implement RoleList API
Transaction Propagation and Isolation in Spring @Transactional
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Remove All Occurrences of a Specific Value from a List
Show Hibernate/JPA SQL Statements from Spring Boot
Spring Boot: Customize the Jackson ObjectMapper
Java – Write to File
Setting a Request Timeout for a Spring REST API
Arrays.asList vs new ArrayList(Arrays.asList())
Using the Map.Entry Java Class
Debug a JavaMail Program
Working With Maps Using Streams
Java Program to Solve any Linear Equations
Java Program to Implement ConcurrentHashMap API
Java Program to Implement ConcurrentSkipListMap API
Spring Security Basic Authentication
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Spring WebClient Filters
Extract links from an HTML page
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Implement Sorted Vector
A Guide to Apache Commons Collections CollectionUtils
Object cloning trong java
Cài đặt và sử dụng Swagger UI
The Spring @Controller and @RestController Annotations
Java Program to Implement Gauss Jordan Elimination
Guide to Guava Table
The Dining Philosophers Problem in Java