# Java random number generator



## GSquadron (Jul 31, 2011)

Hi guys!
I created a program using random numbers and this one seems the easier than the one i build
But the problem is, what if i dont want duplicates! The numbers get used like 2 or 3 times.
Can anyone help me with the code?

```
import java.util.Random;

public class sudoku {
public static void main (String args[]){
Random row = new Random();
int number;

for(int numbers = 0;numbers < 10;numbers++){
number = row.nextInt(10);
System.out.println(number++ + " ");
}
}
}
```


----------



## robn (Jul 31, 2011)

By calling nextInt(10) it will output an integer number between 0 and 9 ...so there will be repetition quickly as that's only ten possibilities!

What kind of numbers did you want it to give you?


----------



## GSquadron (Jul 31, 2011)

If i want i can get millions of possibilities.
The real problem is, i dont want *dublicates*
meaning that if i run the program, it shows the result
and it has 3 twos, four eights etc....
i want to generate for example:
First time i run it, it gives: 1,5,4,2,3,9,6,7,8.
Second time i run it: 3,6,4,8,5,2,1,7,9.
And so on
*not*: 3,3,3,3,4,5,5,4,4
Also, the kind of numbers are integer


----------



## robn (Jul 31, 2011)

Try this:

package randnum;

import java.util.Random;
import java.util.TreeSet;

public class RandNum {
	public static void main(String[] args) {

		Random row = new Random();
		int number;
		TreeSet<Integer> usedNumbers = new TreeSet();

		for (int numbers = 0; numbers < 10; ) {
			number = row.nextInt(10);
			if (usedNumbers.contains(number) == false) {
				usedNumbers.add(number);
				System.out.println(number++ + " ");
				numbers++;
			}
		}
	}
}

Each new number is compared to a set of all the previously selected numbers.


----------



## GSquadron (Aug 1, 2011)

It works perfect!


----------



## robn (Aug 1, 2011)

Cool happy I can help!

By the way I noticed the line...
   System.out.println(number++ + " ");
will add one to "number" after it is printed. Be careful with code like that - it is hard to notice!


----------



## GSquadron (Aug 1, 2011)

In fact i made it :}
I just thought it is better to add a ++ rather than making a +1

Also, what about the multidimensional arrays?
Does it still be int?


----------



## Kreij (Aug 1, 2011)

There is no multi-dimensional array in the code in the OP of this thread. If you are refering to code in another thread you created, you really should just create one thread for this project so people can follow what you are trying to accomplish.


----------



## GSquadron (Aug 1, 2011)

I was asking, if i put arrays does the code still be:

```
TreeSet<Integer> usedNumbers = new TreeSet();
```
Cuz i put arrays and it says error.


----------



## Easy Rhino (Aug 1, 2011)

aleksander, you have to try and work it out on your own. that is how you learn. people will point you in the right direction but should not write your code for you.


----------



## Kreij (Aug 2, 2011)

Aleksander Dishnica said:


> I was asking, if i put arrays does the code still be:
> TreeSet<Integer> usedNumbers = new TreeSet();
> Cuz i put arrays and it says error.



We're always happy to help, but when you change something and are getting errors, we need to see the new code block or it is impossible for us to know what is going on.

Also, please use the [code][/code] tags in your posts. That makes it a lot easier to read.


----------



## GSquadron (Aug 3, 2011)

This is what i wrote about the arrays:


```
package randnum;

import java.util.Random;
import java.util.TreeSet;

public class RandNum {
public static void main(String[] args) {

Random row = new Random();
int number[][]={{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9}};
TreeSet<Array> usedNumbers = new TreeSet();

for (int numbers = 0; numbers < 10; ) {
number[][] = row.nextArray(10);
if (usedNumbers.contains(int number[][]) == false) {
usedNumbers.add(int number[][]);
System.out.println(number[][]++ + " ");
numbers[][]++;
}
}
}
}
```


----------



## Kreij (Aug 4, 2011)

Alexander, I don't mean to offend you, but you have no clue what you are doing and are just pasting in code from what people are suggesting. 
You MUST learn the basic concepts of progarmming logic and the use of the available classes and methods before you try to do something as complex as writing a Sudoku generator/solver.
IMHO, you should start with a game like tic-tac-toe (I think it's called Xs and Os in other places) to get a handle on what it takes to write a more simplistic game before trying to tackle something like the generation and solving algorithms of Sudoku.

As I said, no offense meant. Just trying to help you learn.


----------

