• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java random number generator

Joined
Dec 2, 2009
Messages
3,351 (0.64/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:

robn

New Member
Joined
Mar 9, 2010
Messages
180 (0.03/day)
Location
UK
Processor i7 920 @ 3520MHz locked on 1.04V, all features on
Motherboard Biostar T-Power x58 + ASUS U3S6 + Broadcom WiFi
Cooling Akasa Nero S on the i7 + 2 Akasa Apache case fans
Memory 24Gig Kingston HyperX DDR3
Video Card(s) XFX GTX260 216 XT
Storage 240Gig SanDisk SSD, 750Gig Spinpoint F1
Display(s) BenQ E2200HD
Case Coolermaster Elite 334
Audio Device(s) Realtek(!) with M-Audio AV40 + Wharfedale SW150
Power Supply Antec EarthWatts 650
Software Win7 64 Pro / Fedora KDE
Benchmark Scores wPrime 32M 7.265sec - 3DMark2001 57673 - Intel Linpack 50.6237 GFlops
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?
 
Joined
Dec 2, 2009
Messages
3,351 (0.64/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
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

New Member
Joined
Mar 9, 2010
Messages
180 (0.03/day)
Location
UK
Processor i7 920 @ 3520MHz locked on 1.04V, all features on
Motherboard Biostar T-Power x58 + ASUS U3S6 + Broadcom WiFi
Cooling Akasa Nero S on the i7 + 2 Akasa Apache case fans
Memory 24Gig Kingston HyperX DDR3
Video Card(s) XFX GTX260 216 XT
Storage 240Gig SanDisk SSD, 750Gig Spinpoint F1
Display(s) BenQ E2200HD
Case Coolermaster Elite 334
Audio Device(s) Realtek(!) with M-Audio AV40 + Wharfedale SW150
Power Supply Antec EarthWatts 650
Software Win7 64 Pro / Fedora KDE
Benchmark Scores wPrime 32M 7.265sec - 3DMark2001 57673 - Intel Linpack 50.6237 GFlops
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.
 
Joined
Dec 2, 2009
Messages
3,351 (0.64/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
It works perfect!
 

robn

New Member
Joined
Mar 9, 2010
Messages
180 (0.03/day)
Location
UK
Processor i7 920 @ 3520MHz locked on 1.04V, all features on
Motherboard Biostar T-Power x58 + ASUS U3S6 + Broadcom WiFi
Cooling Akasa Nero S on the i7 + 2 Akasa Apache case fans
Memory 24Gig Kingston HyperX DDR3
Video Card(s) XFX GTX260 216 XT
Storage 240Gig SanDisk SSD, 750Gig Spinpoint F1
Display(s) BenQ E2200HD
Case Coolermaster Elite 334
Audio Device(s) Realtek(!) with M-Audio AV40 + Wharfedale SW150
Power Supply Antec EarthWatts 650
Software Win7 64 Pro / Fedora KDE
Benchmark Scores wPrime 32M 7.265sec - 3DMark2001 57673 - Intel Linpack 50.6237 GFlops
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!
 
Joined
Dec 2, 2009
Messages
3,351 (0.64/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
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:

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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:
 
Joined
Dec 2, 2009
Messages
3,351 (0.64/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
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:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,444 (2.43/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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:
Joined
Dec 2, 2009
Messages
3,351 (0.64/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
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:

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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.
 
Top