 |
05-11-2009, 06:44 AM
|
#1
|
|
ST:T's Resident Otaku
Join Date: Nov 2007
Location: A cave with wifi.
|
Need some JAVA programming help
So I'm taking a JAVA programming class, but I'm not too good with it. We needed to come up with a random number generator that won't come up with the same number twice, and it can use up to 3 digits. But this is a close as I could get:
Code:
import java.util.Random;
/** Generate 10 random integers in the range 0..99. */
public final class RandomInteger {
public static final void main(String... aArgs){
log("Generating 10 random integers in range 0..99.");
//note a single Random object is reused here
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx){
int randomInt = randomGenerator.nextInt(100);
log("Generated : " + randomInt);
}
log("Done.");
}
private static void log(String aMessage){
System.out.println(aMessage);
}
}
Anyone have have any suggestions on how I can tweak this?
EDIT: I'm also using the freeware bluejay to program in JAVA.
__________________
xfire-stix9693
Steam - slygathore
PSN - SLYGATHOR
XBL - S1YGATHOR - (that's a 1 not an L)
R.I.P Christian James Koehler, I'll miss you buddy.
Building a computer is like playing with Legos, the pieces only fit one way.
"If ya gots more feelin's to express, go into the kitchen and bake yourself a bunt cake."
My Minecraft Server: craftserve.dyndns.org
Last edited by pb_freak9666 : 05-11-2009 at 06:51 AM.
|
|
|
05-11-2009, 10:54 AM
|
#2
|
|
UNDERSTAND UNDERSTAND
Join Date: May 2004
Location: so*on
|
Nuclear decay.
Geiger counter and a household smoke detector. 
__________________
______________
|
|
|
05-11-2009, 11:07 AM
|
#3
|
|
ST:T's Resident Otaku
Join Date: Nov 2007
Location: A cave with wifi.
|
What?
__________________
xfire-stix9693
Steam - slygathore
PSN - SLYGATHOR
XBL - S1YGATHOR - (that's a 1 not an L)
R.I.P Christian James Koehler, I'll miss you buddy.
Building a computer is like playing with Legos, the pieces only fit one way.
"If ya gots more feelin's to express, go into the kitchen and bake yourself a bunt cake."
My Minecraft Server: craftserve.dyndns.org
|
|
|
05-11-2009, 12:23 PM
|
#4
|
|
UNDERSTAND UNDERSTAND
Join Date: May 2004
Location: so*on
|
When you use a language's internal random number generator it's 'pseudorandom.' It's generated by software it is not truly random.
Through external hardware you can achieve truly random numbers.
Nuclear decay is random and if you can measure it voila. So get crackin on that nuclear reactor.
Where are you stuck at? I don't know Java but I can provide C pseudocode if that'd help give you ideas.
__________________
______________
|
|
|
05-11-2009, 12:29 PM
|
#5
|
|
ST:T's Resident Otaku
Join Date: Nov 2007
Location: A cave with wifi.
|
What I've wrote so far, is just for a single digit, I need it to be randomized up to 3. And so it can't use the same number sequence twice. Which I have no idea how to do. 
__________________
xfire-stix9693
Steam - slygathore
PSN - SLYGATHOR
XBL - S1YGATHOR - (that's a 1 not an L)
R.I.P Christian James Koehler, I'll miss you buddy.
Building a computer is like playing with Legos, the pieces only fit one way.
"If ya gots more feelin's to express, go into the kitchen and bake yourself a bunt cake."
My Minecraft Server: craftserve.dyndns.org
|
|
|
05-11-2009, 02:11 PM
|
#6
|
|
lo-fi
|
Add generated numbers to a queue, look through the queue to determine if you've already generated that number?
__________________
Shoot the ****: irc.oftc.net #doublethink | dodgyaim
|
|
|
05-11-2009, 02:40 PM
|
#7
|
|
ST:T's Resident Otaku
Join Date: Nov 2007
Location: A cave with wifi.
|
That's the thing, this is as much as I know...
__________________
xfire-stix9693
Steam - slygathore
PSN - SLYGATHOR
XBL - S1YGATHOR - (that's a 1 not an L)
R.I.P Christian James Koehler, I'll miss you buddy.
Building a computer is like playing with Legos, the pieces only fit one way.
"If ya gots more feelin's to express, go into the kitchen and bake yourself a bunt cake."
My Minecraft Server: craftserve.dyndns.org
|
|
|
05-11-2009, 03:10 PM
|
#8
|
|
Need to pay student loans
Join Date: Aug 2007
Location: NJ
|
I was working on something similar but in C++.
Basically here is the pseudocode of the program:
Generate random numbers.
Store the random numbers into an array or however you want to store them.
Have a temp variable that take the value of one of the random numbers.
Check to see if this number exists as a duplicate
If number is not a duplicate, continue to check the rest of the numbers.
If number is a duplicate reassign that variable with a new value.
Hope that makes sense.
Edit: You can also check Google for "Java: non repeating random numbers"
|
|
|
05-11-2009, 06:05 PM
|
#9
|
|
ST:T's Resident Otaku
Join Date: Nov 2007
Location: A cave with wifi.
|
Thanks for the help guys. I got all of it written, with the exeption of an GUI, I don't know how to create a form with just a textbox and button in BlueJ.
__________________
xfire-stix9693
Steam - slygathore
PSN - SLYGATHOR
XBL - S1YGATHOR - (that's a 1 not an L)
R.I.P Christian James Koehler, I'll miss you buddy.
Building a computer is like playing with Legos, the pieces only fit one way.
"If ya gots more feelin's to express, go into the kitchen and bake yourself a bunt cake."
My Minecraft Server: craftserve.dyndns.org
|
|
|
05-11-2009, 09:54 PM
|
#10
|
|
read. comprehend. post.
Join Date: May 2001
Location: Rochester, NY
|
It can't do the same *number* twice or it can't do the same *sequence* twice? The two are very different. If it's number, you'll need to use the queue. If it's sequence, just seed it with the current time.
__________________
the door is open
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|