Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 checking numbers
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
potatoes




PostPosted: Wed Apr 14, 2004 7:46 pm   Post subject: checking numbers

ok the following is *suppose* to check the if all teh values in one column are different. the first column starts from 1 to 15 the next is from 16 to 30 and soo on just like bingo. what it does right now is check finds the values that are alike and changes one of them... except it doesnt change it according to its proper range... well thats wat i think its doing.. can somemone look through it and offer some ideas??

for (int count = 0 ; count < 60 ; count += 15)
{
for (int check = 0 ; check < col ; check += 2)
{

if (board [row] [col] == board [check] [col])
{
board [row] [col] = (int) (Math.random () * 14) + 1 + count;
flag = true;
}
}

}
Sponsor
Sponsor
Sponsor
sponsor
potatoes




PostPosted: Wed Apr 14, 2004 9:01 pm   Post subject: (No subject)

hello...
anyone out there......
potatoes




PostPosted: Wed Apr 14, 2004 9:49 pm   Post subject: (No subject)

ya sooo i have a question the Math.random command thing...is there a way to have expections like this is the range of numbers but dont choose this number from the range????
wtd




PostPosted: Wed Apr 14, 2004 10:17 pm   Post subject: (No subject)

The data you're dealing with is a board, which is composed of columns. So we have two classes. Column is an inner class of Board since we'll not need it outside of the context of a Board.

Column is a comparatively simply class. It holds an array of integers that is a certain number of rows long. Our constructor for that class merely initializes the array and sets all of its data to zero.

It has an allUnique() method which checks to see if all of the values in the column are unique. To accomplish this it loops over the entire array once for each element in the array. In this way it compares all elements to each other, taking care not to compare the same element to itself.

It has a populateWithRandom(int low, int high, boolean unique) method which populates the array with random numbers in a given range. Ifthe unique parameter is true (it's false by default) then the method will ensure that all values are unique.

The Board class merely has a constructor and a method for accessing individual columns.

In the main method, I create a new Board with 4 columns and 15 rows. I then loop over the columns and populate each with random numbers in increasing ranges.

This code is untested.

code:
import java.io.*;
import java.lang.*;

class Exercise {
        public class Board {
                private class Column {
                        private int rows;
                        private int[] data;

                        public Column(int initRows) {
                                rows = initRows;
                                data = new int[rows];

                                for (int i = 0; i < rows; i++)
                                        data[i] = 0;
                        }

                        public boolean allUnique() {
                                for (int i = 0; i < rows; i++)
                                        for (int j = 0; j < rows; j++)
                                                if (i != j && data[i] == data[j])
                                                        return false;
                                return true;
                        }
                       
                        public void populateWithRandom(int low, int high, boolean unique = false) {
                                int range = high - low;
                                for (int i = 0; i < rows; i++) {
                                        int rand;
                                        boolean alreadyUsed = false;
                                       
                                        do {
                                                rand = (int)(Math.random() * range) + low;
                                       
                                                if (unique)
                                                        for (int j = 0; j < i && !alreadyUsed; j++)
                                                                if (data[j] == rand)
                                                                        alreadyUsed = true;
                                        } while (alreadyUsed);
                                                       
                                        data[i] = rand;
                                }                            
                        }
                }

                private int cols, rows;
                private Column[] columns;

                public Board(int initCols, int initRows) {
                        cols = initCols;
                        rows = initRows;
                        columns = new Columns[cols];

                        for (int i = 0; i < cols; i++)
                                columns[i] = new Column(rows);
                }
               
                public Board column(int index) {
                        return columns[index];
                }
        }

        public static void main(String[] args) {
                Board b = new Board(4, 15);
               
                for (int i = 0; i < 4; i++)
                        column(i).populateWithRandom(i * 15, (i * 15) + 15);
        }
}
wtd




PostPosted: Wed Apr 14, 2004 10:19 pm   Post subject: (No subject)

potatoes wrote:
ya sooo i have a question the Math.random command thing...is there a way to have expections like this is the range of numbers but dont choose this number from the range????


The basic algorithm is going to be:

1. Chose a random number from the range.
2. Check to see if it matches any of the numbers already chosen.
3. If it does, go to step 1.
4. Otherwise, store the random number in the array and go on to the next in the array.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: