• 06-28-2009, 06:38 AM
gg2901
I'm new java programming. :confused: So I really need some help. I'm trying to create a program that generates 5 random numbers between 1-9. I got it to do that, but now I need to generate a new number for any duplicate numbers. I've done everything, Its probably really easy, but I dont know how to do it.

import javax.swing.*;
import java.util.Arrays;

public class CodeGenerator
{
String numberStr;
int number;
final int ARRAY_SIZE = 5;
int[] numbers = new int[ARRAY_SIZE];
java.util.Random rand = new java.util.Random();

//Constructor
public CodeGenerator()
{
calculate();
}

public void calculate()
{

for(int index=0; index < numbers.length; index++)
{
numbers[index] = (int)(Math.random()*10);

while (numbers[index] == 0 || numbers[index] == 10)
{
numbers[index] = (int)(Math.random()*10);
}

}

numberStr = Arrays.toString(numbers);
numberStr = numberStr.trim();
numberStr = numberStr.replace("[" , "");
numberStr = numberStr.replace("]", "");
numberStr = numberStr.replace(",", "");
numberStr = numberStr.replace(" ", "");

}
public String getNumString()
{
calculate();
return numberStr;
}

public int getNumNumber()
{
calculate();
number = Integer.parseInt(numberStr);
return number;

}

}
• 06-28-2009, 07:25 AM
angryboy
here's a little pseudo code for the logic
Code:

```LOOP {   LET r = get_random_number();   boolean b = check_for_dups( r );   if b is false{ // meaning no dups     array.add( r );     index++;   }   else loop again with out increasing index } ... boolean check_for_dups(VAR r){   for each array element     does r == element ?     if(yes) return true;     else continue;   if nothing is found, then return false }```
• 06-28-2009, 07:34 AM
sukatoa
suppose you have an array(global declared/field member) of random(unique numbers from 1-9)

You may create a method that checks the number's existency in the array, (returns true or false)

From that, you may do a while loop like:
Code:

```array[]; while( array.stored < 5 ){       generated = generateRandom(1-9);       if( generated not exists in array ){             store generated to array;             array.stored++;       } }```
please do correct me if it doesn't make any sense,
• 06-28-2009, 10:35 PM
gg2901
Thanks so much. I finally got it to work using an array list and using the shuffle method . But now I just need some help conveting my array list to a string? Is there a way to do that? Thats the only thing I need and Im done! =(

import javax.swing.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;

public class CodeGenerator
{

String numberStr;
int number;
java.util.Random rand = new java.util.Random();
ArrayList<Integer> numbers = new ArrayList<Integer>();
int arrayList2;

//Constructor
public CodeGenerator()
{
calculate();
}

public void calculate()
{

for(int index = 0; index < 9; index++)
{
}
Collections.shuffle(numbers); //shuffling all 9 numbers

for(int index2 =0; index2 < 5; index2++)
{
arrayList2 = numbers.get(index2));
System.out.print(arrayList2 + " ");
}

//numberStr = ArrayList.toString(numbers);
//numberStr = numberStr.trim();
//numberStr = numberStr.replace("[" , "");
//numberStr = numberStr.replace("]", "");
//numberStr = numberStr.replace(",", "");
//numberStr = numberStr.replace(" ", "");

}

public String getNumString()
{
calculate();
return numberStr;
}

public int getNumNumber()
{
calculate();
number = Integer.parseInt(numberStr);
return number;

}

}
• 06-28-2009, 11:19 PM
Singing Boyo
try something like this...

Code:

```//pseudocode create string; get Iterator for ArrayList (ArrayList.iterator() returns it) while(the iterator has more elements to iterate through) add element to string; return string```
Also, use code tags ([code] and [/code]) or # button in advanced mode.