Results 1 to 4 of 4

Thread: random numbers

  1. #1
    lynkxs is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default random numbers

    I can't figure out how to compare the array from the main method to the getRandom method.

    Java Code:
    public class Testingg {
    	public static void main(String[] args) {
    
    		//Should print 25
    		System.out.println(getRandom(12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24));
    		
    		//Should print a random number between 1 to 25 except...
    		System.out.println(getRandom(13, 6, 17, 14, 18, 20));
    		System.out.println(getRandom(1, 5, 8, 9));
    
    	}
    	
    	public static int getRandom(int ... numbers) {
    		
    		
    		int randomInt = (1 + (int)(Math.random()*((25-1) + 1)));
    		
    		for(int i = 0; i < numbers.length; i++) {
    			
    			//Keeps generating a random number until it find a random number that is not in the array
    			while(numbers[i] == randomInt) {
    				randomInt = (1 + (int)(Math.random()*((25-1) + 1)));
    			}
    		}
    
    		return randomInt;
    	}
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: random numbers

    Can I suggest that your getRandom method, rather than constantly attempting to generate random numbers until it finds one that isn't in the array, that you create a new array consisting solely of the numbers that aren't in the array, then generate a random number from 0 to the array length -1? That number will be the index into this new array.

    If you still want to stick to the brute force method, then you need the while loop to be the outer loop.
    Java Code:
    while not found
       set found true
       generate random number
       for all numbers in array
          if random number matchs array number set found to false
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    lynkxs is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: random numbers

    For line 1, I have to do a while loop that while the randonInt is not equal to the array?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: random numbers

    Quote Originally Posted by lynkxs View Post
    For line 1, I have to do a while loop that while the randonInt is not equal to the array?
    No, you use a boolean flag to indicate that you haven't generated a suitable random number yet.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 02:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM

Posting Permissions

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