Results 1 to 7 of 7
Thread: Random integer generate
- 06-21-2010, 10:14 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
- 06-21-2010, 11:19 PM #2
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
- 06-21-2010, 11:30 PM #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?
- 06-21-2010, 11:42 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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-21-2010, 11:47 PM #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?
- 06-22-2010, 12:41 AM #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[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
- 06-22-2010, 01:19 AM #7
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
-
generate random letters inbetween a string
By greg677 in forum New To JavaReplies: 1Last Post: 05-04-2010, 05:06 AM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Trying to Generate Random number
By PeterFeng in forum New To JavaReplies: 10Last Post: 01-14-2009, 08:37 AM -
Generate a random number
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:23 AM -
How to generate random number in java
By fernando in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks