Results 1 to 7 of 7
  1. #1
    trbLeeciN is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Random integer generate

    Hi all. I am going to do sudoku game for my term project. what i wanted to ask u guys is about random integer generate.
    My random class generates 1 to 9 numbers. For example i want not to generate 5 in that class. how can i do this operation? any ideas? thank u.

  2. #2
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    PHP Code:
        
          Random ro = new Random();
          int number;
          do{
             number = 1+ ro.nextInt(9);
          }
          while(number == 5);
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  3. #3
    trbLeeciN is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    but whenever i generate new integer, next integer must not be my previous integers. What i tried to ask. random generates 1,2,3,4,5,6,7,8,9 normally. Am i able to do range of random like 1,2,3,5,6,7,8,9 ?
    what i tried to ask without doing any do-while/while/if am i able to change the range of random class?

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    You're not actually dealing with logical numbers here even though it seems like you are since these numbers/symbols/tokens perform no numeric calculations. Instead you're dealing with 9 constants with specific rules of distribution. I think that your Sudoku cells must each understand and obey the rules of distribution: one way is that they can check their row, column and small square for numbers already selected eliminating them from say an ArrayList<Integer> and then select randomly from the reduced list. There will only be a limited way that these can be selected and be valid -- in other words if you do it this way, you'll often paint yourself into a corner where there are no viable numbers to choose from, and you'll have to abandon that attempt and retry.

  5. #5
    trbLeeciN is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    hmm. so u are saying that create an arraylist inclueds numbers 1 to 9 and select random numbers. when random number selected, erase the selected random from arraylist? am i right? if i am, how can i select random numbers from an arraylist and erase its selected number?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    An arraylist is perhaps more than is needed. How about an array of booleans.
    Set the entry true when that number is used. The while loop would be controlled by whether the entry for the returned number was true or not.
    Java Code:
     boolean[]  usedNbr = new boolean[10];  // make it one based, initialized to false
    
     Random ro = new Random();
      ...
          int number;
          do{
             number = 1+ ro.nextInt(9); // get a number from 1 to 9
          }
          while(usedNbr[number]);   // continue if the number has been used
    
          usedNbr[number] = true;   // remember that the number has been used

  7. #7
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    put this in a class



    PHP Code:
        int[] selected = new int[9]; // 0-8 blank
    
        public int getRandom(){
    
             int randomNumber =0;
             int arrayUsed;
             boolean used = false;           
             Random ro = new Random();  //create object from random class
    
    
             do{
                randomNumber = 1+ro.nextInt(9);      //make random number between 1 and 9
                arrayUsed = randomNumber -1;         //we store the random number in the array below. 
                                                                  //ie if the random number is 9 then it gets put into selected[8]
    
    
                    if (selected[arrayUsed]== 0){                      //if the array is empty, put the random number in there
                        selected[arrayUsed] = randomNumber;
                        used = true;                                         //set to true so program breaks from while loop and returns the random number
    
                    }else
                        used=false;                                         //if that array/random number already been used then redo the while loop to get a 
             }                                                                  //different number
             while(used==false);
    
             return randomNumber;                                      // unique random number outputed
        }

    this is how you call it and get the results. the below code assumes you called the above class GUI.

    The code below calls the method 9 times, each time getting a unique random number between 1 and 9 and then it stores it in the array randomArray.

    hope this helps with what your doing


    PHP Code:
        public static void main(String[] args) {
    
            GUI fc = new GUI();
    
            int[] randomArray = new int[9];    //keep this at 9 (i didnt do any error handling for larger numbers because program doesnt require it)
    
            for(int i = 0;i < randomArray.length;i++){
                randomArray[i] = fc.getRandom();           //this array gets filled up with random numbers between 1 and 9 that are all unique
                System.out.println(randomArray[i]); //check results in console
            }
    
        }
    Last edited by alacn; 06-22-2010 at 01:29 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

Similar Threads

  1. generate random letters inbetween a string
    By greg677 in forum New To Java
    Replies: 1
    Last Post: 05-04-2010, 05:06 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 08:37 AM
  4. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:23 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:32 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
  •