Friday, 17 November 2017

Java Program to Generate Random Numbers Using Multiply with Carry Method


Code:

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
        {
            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:

The random number sequence is: 795 382 1487 260 475 1798 1347 722 1389 63



More Java Programs:












100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...