![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 11
Rep Power: 0
![]() |
fill array with unique numbers
fill Array with unique number
i have to construct an array of integers and i have to which will generate six integer values in the range 1 to 45 inclusive. The algorithm must loop until all of the six numbers generated are unique for now i ahve only be able to do that part>> import java.util.Random; class s1_1999 { private static int[] fillArray(int [] array) { Random random = new Random(); for(int row = 0 ; row < array.length ;row++) { array[row]=random.nextInt(10); } private static boolean compare(int [] testarray) { boolean test=false; int testValue=0; for(int row = 0; row<testarray.length;row++) { for (int i=1 ;i<testarray.length ; i++) { if(testarray[row]==testarray[i]) { test=true; } } return test; } public static void main(String []args) { int [] inarray=new int[6]; fillArray(inarray); } can you help me to complete this java program plz?????????? thanx in advance from andy Forum: Java Post New Thread Logged in as secrecy230 Title: Message: |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
for(int row = 0 ; row < array.length ;row++)
{ array[row]=row; } There you have it, they're all unique.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2005
Posts: 11
Rep Power: 0
![]() |
hey thanx
but the number has to be random so when i apply what u told me it is not random infact the overall question is Write a pseudo code algorithm which will generate six integer values in the range 1 to 45 inclusive. The algorithm must loop until all of the six numbers generated are unique (i.e. none two numbers are the same). You may assume that you have access to the following methods: double Math.random() Generates a random number between 0.0 and 1.0 inclusive. boolean NoneTheSame( int [] numbers) Returns true if all the numbers in the array numbers are unique and false otherwise. |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
That says nothing about all the numbers having to be random.
Also, I'm not trying to be mean, but could you please use less abbreviations like "u"? It makes your post harder to read. Thanks. Here's what you could do if they have to be random, I don't know Java very well though so it might not compile void fill_array(int[] array)
{
do
{
for(int i=0;i<6;i++)
{
array[i] = Math.random()*45;
}
}while(!NoneTheSame(array));
}
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#5 |
|
Programmer
|
Hmm.. not the most efficient algorithm by any means. It works, certainly. I'm not trying to rag on Uman, but there are other ways. Most notably, instead of filling the entire array and checking for duplicates at the end, pick each number individually. Also, use a second array to check for collisions instead of using nested for-loops. (Nested loops run in n-squared time, while an array method would run in constant time.. sorta).
public static int getRandom(int min, int max) {
return ((int) (Math.random() * (max - min + 1))) + min;
}
public static void fillArray(int[] toFill) {
boolean[] beenUsed = new boolean[46];
for (int i = 0; i < 45; i++) {
beenUsed[i] = false;
}
//for each element in the array to me filled
for (int j = 0; j < toFill.length; j++) {
//find a number that's not been used yet
int ran;
do {
ran = getRandom(1, 45);
} while (beenUsed[ran]);
//put that number in the toFill array, and mark it used in the beenUsed array
toFill[j] = ran;
beenUsed[ran] = true;
}
}If you're wondering, the boolean array is in fact one size larger than it really needs to be. I could have make it one size smaller and subtraced all values by one, but this way's easier to understand. And before anyone gets on my case over my sacrificing memory efficiency for speed efficienfy, just know that I always favor speed. *shrugs*
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|