1. Member
Join Date
Jun 2009
Posts
2
Rep Power
0

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;

}

}

2. here's a little pseudo code for the logic
Java Code:
```LOOP
{
LET r = get_random_number();
boolean b = check_for_dups( r );
if b is false{ // meaning no dups
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
}```

3. 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:
Java 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,
Last edited by sukatoa; 06-28-2009 at 07:36 AM.

4. Member
Join Date
Jun 2009
Posts
2
Rep Power
0
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;

}

}
Last edited by gg2901; 06-28-2009 at 10:52 PM. Reason: I accidentially pressed enter before I was done with the reply.

5. Senior Member
Join Date
Mar 2009
Posts
552
Rep Power
9
try something like this...

Java Code:
```//pseudocode
create string;
get Iterator for ArrayList (ArrayList.iterator() returns it)
while(the iterator has more elements to iterate through)

return string```
Also, use code tags ([code] and [/code]) or # button in advanced mode.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•