954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

using boolean expression on an array

Hi

I have created an array of random numbers but I need to stop duplicate numbers being added to the array so I have added the boolean 'fresh'. however, I somehow need to tell my 'fresh' to only check the number of elements added to the array so far and I am embarrassed to say i can't figure this out as I am sure it is easy.

I have been searching books and the internet but just cannot figure it out. :rolleyes:

Any help in the right direction would be really appreciated

my code is:

int amount = 7;   // number of integers needed to generate
int[] nums = new int[amount]; 
int i;
	 
// method for searching the array for duplicate numbers
boolean fresh (int [] nums, int val){
   for (int i = 0; i < amount; i++){
      if (nums[i] == val) return true;
      }
     {
         return false;
         }
}// end fresh method
	 
//method to generate random numbers to insert into the tree
public void randomNumber()
   {
    do
     {
       for (int i=0; i<amount; i++) { 
       nums[i] = (int) Math.floor(Math.random() * 100);             
       nums[i] = nums[i] -50; 
       System.out.println("random is " + nums[i]);
       }
       }   
      while(!(fresh(nums,i)));
}// end of randomNumber method
JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

That can be done a lot easier.

List<Integer> l = new ArrayList<Integer>();
        for (int i=0;i<amount;i++) {
            int q = (int) Math.floor(Math.random() * 100);
            if (!l.contains(q)) {
                l.add(q);
            }            
        }
        Integer[] dummy = l.toArray(new Integer[] {});


You'll end up with an array of Integer instead of int but with autounboxing that doesn't matter.

Most books won't mention tricks like this, and older books won't even mention the language features used here because they didn't exist until a few months ago.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Hi

I have created an array of random numbers but I need to stop duplicate numbers being added to the array so I have added the boolean 'fresh'. however, I somehow need to tell my 'fresh' to only check the number of elements added to the array so far and I am embarrassed to say i can't figure this out as I am sure it is easy.

I have been searching books and the internet but just cannot figure it out. :rolleyes:

Any help in the right direction would be really appreciated

my code is:

int amount = 7;   // number of integers needed to generate
int[] nums = new int[amount]; 
int i;
	 
// method for searching the array for duplicate numbers
boolean fresh (int [] nums, int val){
   for (int i = 0; i < amount; i++){
      if (nums[i] == val) return true;
      }
     {
         return false;
         }
}// end fresh method
	 
//method to generate random numbers to insert into the tree
public void randomNumber()
   {
    do
     {
       for (int i=0; i<amount; i++) { 
       nums[i] = (int) Math.floor(Math.random() * 100);             
       nums[i] = nums[i] -50; 
       System.out.println("random is " + nums[i]);
       }
       }   
      while(!(fresh(nums,i)));
}// end of randomNumber method

First of all, you send to the fresh method the index 'i' and not the value nums[i]. A solution I think:

int amount = 7;   // number of integers needed to generate
int[] nums = new int[amount]; 
int i;
	 
// method for searching the array for duplicate numbers
boolean fresh (int [] nums, int val, int index){
   for (int i = 0; i < index; i++){
      if (nums[i] == val) return true;
      }
     {
         return false;
         }
}// end fresh method
	 
//method to generate random numbers to insert into the tree
public void randomNumber()
   {
    do
     {
       for (int i=0; i<amount; i++) { 
       nums[i] = (int) Math.floor(Math.random() * 100);             
       nums[i] = nums[i] -50; 
       System.out.println("random is " + nums[i]);
       }
       }   
      while(!(fresh(nums, nums[i], i)));
}// end of randomNumber method
adi1600
Newbie Poster
3 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Hi adi1600

I tried your suggestion, but the loop doesn't stop! I take it that's because I haven't told it what index is, so it doesn't know when to stop looping?

Any further suggestions on this greatly appreciated :)

JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Hi Jwenting

thanks for the reply. I tried your code but couldn't get it to compile as i wasn't sure if you meant just replace my two methods with this new code or not?

So I tried:

public void randomNumber()
	    {
		  List<Integer> l = new ArrayList<Integer>();
	      for (int i=0;i<amount;i++) {
	          int q = (int) Math.floor(Math.random() * 100);
	          if (!l.contains(q)) {
	              l.add(q);
	          }            
	      }
	      Integer[] dummy = l.toArray(new Integer[] {});
	 }// end of randomNumber method


but it says several errors, one of which is arraylist cannot be resolved to type.

Any ideas?

Thanks

JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Yes, of course, I did not tested, you should try the following randomNumber method:

public void randomNumber() {
        int i;
        do {
            for (i = 0; i < amount; i++) {
                nums[i] = (int) Math.floor(Math.random() * 100);
                nums[i] = nums[i] - 50;
                System.out.println("random is " + nums[i]);
            }
        } while (i < amount && !(fresh(nums, nums[i], i)));
    }
adi1600
Newbie Poster
3 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Hi

Thanks for keep trying with this. Just crashed that and it also crashed out.

Any further help would be appreciate - thank you for your time :mrgreen:

JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Your solution is wrong anyway, this should work (I even tested before posting :) ):

package ro.csc.jccg;

/**
 * Created by IntelliJ IDEA.
 * User: Adrian Cioaca
 * Date: Aug 3, 2005
 * Time: 2:21:19 PM
 */
public class Test {

    int amount = 99;   // number of integers needed to generate
    int[] nums = new int[amount];
    int i;

    // method for searching the array for duplicate numbers
    boolean fresh(int[] nums, int val, int index) {

        for (int i = 0; i < index; i++) {
            if (nums[i] == val) return false;
        }

        return true;

    }// end fresh method

    //method to generate random numbers to insert into the tree
    public void randomNumber() {
        int i;
        //do {
        for (i = 0; i < amount; i++) {
            int aux = (int) Math.floor(Math.random() * 100);
            aux = aux - 50;
            if (fresh(nums, aux, i)) {
                System.out.println("random is " + aux);
                nums[i] = aux;
            } else {
                i--;
                //break;
            }
        }        
        //} while (i < amount && !(fresh(nums, nums[i], i)));
    }// end of randomNumber method

    public static void main(String[] args) {

        Test test = new Test();

        test.randomNumber();
    }
}
adi1600
Newbie Poster
3 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Hi Jwenting

thanks for the reply. I tried your code but couldn't get it to compile as i wasn't sure if you meant just replace my two methods with this new code or not?

but it says several errors, one of which is arraylist cannot be resolved to type.

Any ideas?

Thanks

Yes, that replaces all the code you use to create the array.
I didn't bother to post the import statements you need, leaving that up to your imagination.
Code only works with the latest compiler also. While you can get it working with older compilers, it takes some work and isn't as easy to use.
Of course you will need to do something with the array once you've created it also :)

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

thanks very much adi1600 - have used your code and it definitely looks better than mine(well it works for starters!) :cheesy:

Appreciate your help.

JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

HI Jwenting

Thanks for the response, thought I needed to import something but it might also be I'm out of date ;)

Thanks again

JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

hi,
I believe you need to have an array of random numbers which are unique. Hope this code helps.

The logic that i have used is i prepopulate an array of sequential numbers and swap the indices randomly as many times as the size of the array. If you donot do it this way and populate a collection u might end up in generating a lot of random random numbers at the end which has of a lot of chance to be a duplicate one.

here goes the code:

[HTML]
class RandomArray
{
public static void main(String args[])
{
try
{
int arraySize=Integer.parseInt(args[0]);
int randArray[]=new int[arraySize];
//populating the array with a sequential numbers
for(int i=0;i");
}
}
}
[/HTML]
hope that helps...
regards
Srinivas

cheenu78
Light Poster
45 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Hi

Thanks very much for your sample code - very helpful :lol:

JavaFish
Newbie Poster
19 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Can this be modified to include different parameters of the number of random numbers to be generated depending on which radio button is selected? I can't get it to work.

hjangel
Newbie Poster
6 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You