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``````

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)) {
}
}
Integer[] dummy = l.toArray(new Integer[] {});``````

You'll end up with an array of Integer instead of int but with autounboxing …

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 …

## All 13 Replies

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)) {
}
}
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.

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. 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``````

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 :)

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)) {
}
}
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

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)));
}``````

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:

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

``````package ro.csc.jccg;

/**
* Created by IntelliJ IDEA.
* 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();
}
}``````

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 :)

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

HI Jwenting

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

Thanks again

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:

``````class RandomArray
{
public static void main(String args[])
{
try
{
int arraySize=Integer.parseInt(args);
int randArray[]=new int[arraySize];
//populating the array with a sequential numbers
for(int i=0;i<arraySize;i++)
{
randArray[i]=i+1;
}
//printing the array which will be sequential
System.out.println("before.........");
for(int i=0;i<arraySize;i++)
{
System.out.println(randArray[i]);
}

//swapping the two randon indexes
for(int i=1;i<arraySize+1;i++)
{
int temp1=(int)Math.floor((Math.random()*arraySize));
int temp2=(int)Math.floor((Math.random()*arraySize));
int temp3=randArray[temp1];
randArray[temp1]=randArray[temp2];
randArray[temp2]=temp3;
}
System.out.println("after.........");
for(int i=0;i<arraySize;i++)
{
System.out.println(randArray[i]);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
//System.out.println("usage\njava RandomArray <size_of_the_array>");
}
}
}``````

hope that helps...
regards
Srinivas

Hi