• File: RandomNumbers.java
  • Full Path: /home/lmnet/fm-site/files/JavaProgramming/JAVAPROGRAMS/RandomNumbers.java
  • Date Modified: 01/16/2026 5:50 AM
  • File size: 3.55 KB
  • MIME-type: text/x-java
  • Charset: utf-8
 
Open Back
//Code By Regie Buo

import javax.swing.*;
public class RandomNumbers {

    static int randomNum(int Start, int End) {
        if (Start > End) {
            System.out.println("Error, Please use valid Method Parameter. randomNum(Min, Max)");
            System.exit(0);
        }
        int start = Start;
        int end = End;
        if ((Start == 0 || Start == -1) && (End == 1 || End == 0)) {
            start = 1000;
            end = 1001;
        }
        end += 1;
        boolean stop = false;
        long random = 0;
        long random1 = end-start + 1; 
        long seed = System.currentTimeMillis();
        int count = start;
        int Iteration = 0;

        while (stop == false) {
            long time = System.currentTimeMillis()^System.nanoTime();
            seed += Runtime.getRuntime().freeMemory()*time%random1^time*time*(random1);
            random1 += end*time*time^System.nanoTime();
            random = seed%random1-1;
            if (random == 0 || random1 == 0) {
                random = 2*seed%time;
            }
           // System.out.println("Seed:  "+seed+" = "+random);
            if (count == end) {
                seed = time%random-random1+end;
                random = seed%count;
                count = start;
            }
          // System.out.println("\nDebug1 : \nseed = "+seed+" \nRandom = "+random+" \nRandom1 = "+random1+"\n");
            if (Iteration == 10000) {
                if (time%end==0) {
                    random1 = time%start;
                    random = start+1;;
                } else {
                   random = start+((time%end)%3);
                    while (random > end ){
                        random--;
                    }
                //  System.out.println("10,000 iteration and the number is "+ random);
                }
            }
            if ((random >= start) && (random <= end)) {
                if ((Start == 0 || Start == -1) && (End == 1 || End == 0)) 
                {
                if (Start == -1 && random == 1000) {
                    random = -1;
                }
                if (Start == 0 && random == 1000) {
                    random = 0;
                }
                if (End == 0 && random == 1001) {
                    random = 0;
                }
                if (End == 1 && random == 1001) {
                    random = 1;
                }
                }
                stop = true;
              //  System.out.println();
             //   System.out.println("Random Number Generated.");
             //   System.out.println();
            }
            if (Iteration == 20000){
                System.out.println("Iteration Exceeded to 20,000, This Algorithm is Bad, The number now is "+random);
                stop = true;
            }
            count++;
            Iteration++;
        }
        int n = (int) random;

        return n;
   }
    public static void main(String[] Args) {
        int random = randomNum(1,10);
        int guest = Integer.parseInt(JOptionPane.showInputDialog("Guest the Number between 1 to 10.\nWhat is the number?"));
        if (guest == random) {
             JOptionPane.showMessageDialog(null, "Congratulations, You Guest The Number");
        } else {
            JOptionPane.showMessageDialog(null, "Wrong! The Number is "+ random);
        }

        int a = 0;
       System.out.println("Random Numbers: ");
        while (a<10) {
            System.out.print(randomNum(1, 10)+", ");
            a++;
        }

    }
}