Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How can I remove an integer from a random number generator?

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How can I remove an integer from a random number generator?

    If a randomly generated number is called once, how can I exclude it so it isn't called next time?

    Please run my code:

    HTML Code:
    package teacher;
    
    public class Driver {
    
    	public static void main(String[] args) {
    		
    		Class x = new Class();
    		x.matrix();
    	}
    
    }
    HTML Code:
    package teacher;
    
    import java.util.Random;
    
    public class Class {
    
    	int i, j;
    	boolean [][] s = new boolean[6][6];
    	Random r = new Random();
    
    	{
    	s[0][0] = false; s[0][1] = true; s[0][2] = false; s[0][3] = true; s[0][4] = false; s[0][5] = false;
    	s[1][0] = false; s[1][1] = true; s[1][2] = false; s[1][3] = false; s[1][4] = false; s[1][5] = true;
    	s[2][0] = true; s[2][1] = false; s[2][2] = true; s[2][3] = true; s[2][4] = true; s[2][5] = true;
    	s[3][0] = true; s[3][1] = false; s[3][2] = true; s[3][3] = false; s[3][4] = false; s[3][5] = true;
    	s[4][0] = false; s[4][1] = true; s[4][2] = false; s[4][3] = true; s[4][4] = true; s[4][5] = true;
    	s[5][0] = false; s[5][1] = true; s[5][2] = true; s[5][3] = false; s[5][4] = true; s[5][5] = false;
    	}
    	
    	public Class(){}
    
    	public void matrix(){
    		
    		for(j=0;j<6;j++){
    			i = r.nextInt(6);
    			while(s[i][j] = true){
    				if(i == 0){
    					System.out.println((j+1) + " period: Teach kindergarden.");
    					//this line is supposed to remove 0 from the generator
    				}
    				if(i == 1){
    					System.out.println((j+1) + " period: Teach " + i + "st grade.");
    					//this line is supposed to remove 1 from the generator
    				}
    				if(i == 2){
    					System.out.println((j+1) + " period: Teach " + i + "nd grade.");
    					//this line is supposed to remove 2 from the generator
    				}
    				if(i == 3){
    					System.out.println((j+1) + " period: Teach " + i + "rd grade.");
    					//this line is supposed to remove 3 from the generator
    				}
    				if(i == 4){
    					System.out.println((j+1) + " period: Teach " + i + "th grade.");
    					//this line is supposed to remove 4 from the generator
    				}
    				if(i == 5){
    					System.out.println((j+1) + " period: Teach " + i + "th grade.");
    					//this line is supposed to remove 5 from the generator
    				}
    				else{}
    				
    				break;
    			}
    		}
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I remove an integer from a random number generator?

    If a randomly generated number is called once, how can I exclude it so it isn't called next time?
    Are you asking how to get unique (not repeated) random numbers?
    A couple of ideas:
    Have the number generator remember what it has returned in the past and not return it again. That would require a list of some sort.
    Another idea: Create a list of unique numbers in order and shuffle it into random order. Remove and return the first number each time the next number is requested.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I remove an integer from a random number generator?

    I've tried to make an arraylist and add all possible integers, but my code gives me an error:

    HTML Code:
    	ArrayList<Integer> x = new ArrayList<Integer>();
    	x.add(0);
    	x.add(1);
    	x.add(2);
    	x.add(3);
    	x.add(4);
    	x.add(5);
    the errors are on the add functions, it is telling me that an '@' is expected

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I remove an integer from a random number generator?

    The code works for me when inside of a method. Where is your code placed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I remove an integer from a random number generator?

    It worked. However, I still can't remove the random integer from the generator.

    --- Update ---

    Please run this code

    HTML Code:
    package teacher;
    
    public class Driver {
    
    	public static void main(String[] args) {
    		
    		Class x = new Class();
    		x.matrix();
    	}
    
    }
    HTML Code:
    package teacher;
    
    import java.util.Random;
    import java.util.ArrayList;
    
    public class Class {
    
    	public Class(){}
    	int i, j;
    	boolean [][] s = new boolean[6][6];
    
    	{
    	s[0][0] = false; s[0][1] = true; s[0][2] = false; s[0][3] = true; s[0][4] = false; s[0][5] = false;
    	s[1][0] = false; s[1][1] = true; s[1][2] = false; s[1][3] = false; s[1][4] = false; s[1][5] = true;
    	s[2][0] = true; s[2][1] = false; s[2][2] = true; s[2][3] = true; s[2][4] = true; s[2][5] = true;
    	s[3][0] = true; s[3][1] = false; s[3][2] = true; s[3][3] = false; s[3][4] = false; s[3][5] = true;
    	s[4][0] = false; s[4][1] = true; s[4][2] = false; s[4][3] = true; s[4][4] = true; s[4][5] = true;
    	s[5][0] = false; s[5][1] = true; s[5][2] = true; s[5][3] = false; s[5][4] = true; s[5][5] = false;
    	}
    
    	public void matrix(){
    		
    		ArrayList<Integer> x = new ArrayList<Integer>();
    
    		x.add(0);
    		x.add(1);
    		x.add(2);
    		x.add(3);
    		x.add(4);
    		x.add(5);
    		
    		Random r = new Random();
    		
    		for(j=0;j<5;j++){
    			i = x.get(r.nextInt(6));
    			while(s[i][j]){
    				if(i==0){
    					System.out.println((j+1) + " period: Teach kindergarden.");
    					x.remove(new Integer(0));
    				}
    				if(i==1){
    					System.out.println((j+1) + " period: Teach " + i + "st grade.");
    					x.remove(new Integer(1));
    				}
    				if(i==2){
    					System.out.println((j+1) + " period: Teach " + i + "nd grade.");
    					x.remove(new Integer(2));
    				}
    				if(i==3){
    					System.out.println((j+1) + " period: Teach " + i + "rd grade.");
    					x.remove(new Integer(3));
    				}
    				if(i==4){
    					System.out.println((j+1) + " period: Teach " + i + "th grade.");
    					x.remove(new Integer(4));
    				}
    				if(i==5){
    					System.out.println((j+1) + " period: Teach " + i + "th grade.");
    					x.remove(new Integer(5));
    				}
    				else{
    					System.out.println();
    				}
    				
    				break;
    			}
    		}
    	}
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I remove an integer from a random number generator?

    remove the random integer from the generator.
    The code should use the shuffled list of numbers for the source of its random numbers. Each number would be removed from the list as it is used.
    You need to write a method to handle all that. Your code would replace the Random class's methods as the source of random numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I remove an integer from a random number generator?

    How?

    --- Update ---

    What does shuffling have to do with removing an int from the generator?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I remove an integer from a random number generator?

    What does shuffling have to do with removing an int from the generator?
    What is the problem you are trying to solve?
    I assumed it was to have a method to get unique, non repeating random numbers in the range 0 to 5.
    My suggestion was that you write a method that returns a number in the desired range with the condition that each number returned would be unique. To do that make a list with the 6 digits and shuffle it. Then have a method that removed and returned the next digit from the list.

    Think about what happens when you deal cards from a shuffled deck of cards. The result is 52 random cards being dealt.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JFrame + JTextField and Random Number Generator
    By Richard_Harrow in forum AWT / Java Swing
    Replies: 7
    Last Post: December 8th, 2013, 05:30 PM
  2. Random Number Generator
    By Rugby_Thompson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2013, 12:58 AM
  3. Random Number Generator Always gives 0
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 03:09 PM
  4. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 12:00 PM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM

Tags for this Thread