• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Java random number generator

Joined
Dec 2, 2009
Messages
3,353 (0.59/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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?
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++ + " ");
}
}
}
 
Last edited:
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?
 
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
 
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.
 
It works perfect!
 
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!
 
In fact i made it :}
I just thought it is better to add a ++ rather than making a +1
:P
Also, what about the multidimensional arrays?
Does it still be int?
 
Last edited:
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. :toast:
 
I was asking, if i put arrays does the code still be:
Code:
TreeSet<Integer> usedNumbers = new TreeSet();
Cuz i put arrays and it says error.
 
Last edited:
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.
 
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. :)
 
Last edited by a moderator:
This is what i wrote about the arrays:

Code:
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[][]++;
}
}
}
}
 
Last edited:
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.
 
Back
Top