1. Member Join Date
Jun 2010
Posts
3
Rep Power
0

## 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.  Reply With Quote

2. ## PHP Code:
```
Random ro = new Random();
int number;
do{
number = 1+ ro.nextInt(9);
}
while(number == 5);```  Reply With Quote

3. Member Join Date
Jun 2010
Posts
3
Rep Power
0

## 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?  Reply With Quote

4. Senior Member Join Date
May 2010
Posts
436
Rep Power
11

## 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.  Reply With Quote

5. Member Join Date
Jun 2010
Posts
3
Rep Power
0

## 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?  Reply With Quote

6. ## 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;  // 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```  Reply With Quote

7. ## put this in a class

PHP Code:
```    int[] selected = new int; // 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

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;    //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.  Reply With Quote

#### Posting Permissions

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