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.

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?

06-22-2010, 12:42 AM

curmudgeon

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.

06-22-2010, 12:47 AM

trbLeeciN

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?

06-22-2010, 01:41 AM

Norm

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.

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

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

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)