Results 1 to 5 of 5
  1. #1
    OberSchuze is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Random selection

    Hello, I am trying to create a random number selector for a 1 to 24 set. This is the code that I have come up with for creating a random seed but it does not seem to work. Could anyone tell me what I am doing wrong please.

    Java Code:
    import java.util.*;
    
    
    public class randomgen{
      public static void main(String[] args){
      Random rand = new Random();
      int num1 = rand.nextInt(24);
      System.out.println("Generated Random Number between 0 to 24 is : " + num1);
      int num2 = rand.nextInt(num1);
      System.out.println("Generated Random Number from seed 1 is : " + num2);
      int num3 = rand.nextInt(num2);
      System.out.println("Generated Random Number from seed 2 is : " + num3);
      }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: Random selection

    The parameter to the nextInt(...) method is not the seed. Read the API.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    OberSchuze is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Random selection

    I rebuilt the code, here:

    Java Code:
    	public class randomgen {
    
    	    private static void doRawRandomNumber() {
    
    	        int rawRandomNumber;
    	        int min = 1;
    	        int max = 18;
    
    	        for (int i = 0; i < 500; i++) {
    	            rawRandomNumber = (int) (Math.random() * (max - min + 1) ) + min;
    	            System.out.println("Random number : " + rawRandomNumber);
    	        }
    
    	        System.out.println("\n");
    	    }
    
    
    	    /**
    	     * Sole entry point to the class and application.
    	     * @param args Array of String arguments.
    	     */
    	    public static void main(String[] args) {
    	        doRawRandomNumber();
    	    }
    
    	}
    But now when I test it running in debug I get this message in console: Error occurred during initialization of VM
    java/lang/NoClassDefFoundError: java/lang/ref/FinalReference


    EDIT: NM figured it out.
    Last edited by OberSchuze; 01-16-2013 at 04:39 PM. Reason: fixed issue.

  4. #4
    OberSchuze is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Random selection

    Here is my final code generation it works great. Now on to using the time variable to power the random number trigger time.

    Java Code:
    public class randomgen {
    
    	    private static void doRawRandomNumber() {
    
    	        int rawRandomNumberCB;
    	        int mincb = 1;
    	        int maxcb = 18;
    	        
    	        int rawRandomNumberT;
    	        int mint = 6;
    	        int maxt = 30;
    
    	        for (int i = 0; i < 500; i++) {
    	            rawRandomNumberCB = (int) (Math.random() * (maxcb - mincb + 1) ) + mincb;
    	            rawRandomNumberT = (int) (Math.random() * (maxt - mint + 1) ) + mint;
    	            System.out.printf("Random Time and Circuit Breaker:\n"
    	                    + "Circuit Breaker: " + rawRandomNumberCB
    	                    +"\n"
    	                    + "Time: " + rawRandomNumberT + "\n");
    	        }
    
    	    }
    
    
    	    /**
    	     * Sole entry point to the class and application.
    	     * @param args Array of String arguments.
    	     */
    	    public static void main(String[] args) {
    	        doRawRandomNumber();
    	    }
    
    	}
    Last edited by OberSchuze; 01-16-2013 at 05:41 PM. Reason: fixed a code error

  5. #5
    OberSchuze is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Random selection

    I am now trying to add a timer function to my file here so that after the 5th number set is generated it stops. I am not sure what I am doing wrong as what I have written has not tossed up any errors. Any help would be appreciated.


    Java Code:
    import java.util.TimerTask;
    
    public class randomgen {
    
        class MyTask extends TimerTask{
        	private int times = 0;
        	
        	public void run() {
        		times++;
        		if (times <=5){
        			
        		}
        		else {
        			this.cancel();
        		}
        	}
        }
    	    private static void doRawRandomNumber() {
    	        int rawRandomNumberCB;
    	        int mincb = 1;
    	        int maxcb = 18;
    	        
    	        int rawRandomNumberT;
    	        int mint = 6;
    	        int maxt = 30;
    
    	        for (int i = 0; i < 500; i++) {
    	            rawRandomNumberT = (int) (Math.random() * (maxt - mint + 1) ) + mint;
    	            try{
    	            Thread.sleep(rawRandomNumberT * 1000);
    	            } catch (InterruptedException e) {}
    	        	rawRandomNumberCB = (int) (Math.random() * (maxcb - mincb + 1) ) + mincb;
    
    	            System.out.printf("Random Time and Circuit Breaker:\n"
    	                    + "Circuit Breaker: " + rawRandomNumberCB
    	                    +"\n"
    	                    + "Time: " + rawRandomNumberT 
    	                    + "\n");
    	        }
            }
    	    /**
    	     * Sole entry point to the class and application.
    	     * @param args Array of String arguments.
    	     */
            MyTask t = new MyTask();
    	    public static void main(String[] args) {
    	        doRawRandomNumber();
    	    }
    
    
    	}

Similar Threads

  1. Replies: 0
    Last Post: 01-23-2012, 09:12 AM
  2. Random selection of words
    By netspinner in forum New To Java
    Replies: 3
    Last Post: 12-14-2010, 05:31 PM
  3. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •