Results 1 to 5 of 5
Thread: Random selection
- 01-15-2013, 04:25 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Random selection
Hello, I am trying to create a random number selector for a 1 to 24 set. This is the code that I have come up with for creating a random seed but it does not seem to work. Could anyone tell me what I am doing wrong please.
Java Code:import java.util.*; public class randomgen{ public static void main(String[] args){ Random rand = new Random(); int num1 = rand.nextInt(24); System.out.println("Generated Random Number between 0 to 24 is : " + num1); int num2 = rand.nextInt(num1); System.out.println("Generated Random Number from seed 1 is : " + num2); int num3 = rand.nextInt(num2); System.out.println("Generated Random Number from seed 2 is : " + num3); } }
- 01-15-2013, 06:46 PM #2
Re: Random selection
The parameter to the nextInt(...) method is not the seed. Read the API.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-16-2013, 04:36 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Re: Random selection
I rebuilt the code, here:
But now when I test it running in debug I get this message in console: Error occurred during initialization of VMJava Code:public class randomgen { private static void doRawRandomNumber() { int rawRandomNumber; int min = 1; int max = 18; for (int i = 0; i < 500; i++) { rawRandomNumber = (int) (Math.random() * (max - min + 1) ) + min; System.out.println("Random number : " + rawRandomNumber); } System.out.println("\n"); } /** * Sole entry point to the class and application. * @param args Array of String arguments. */ public static void main(String[] args) { doRawRandomNumber(); } }
java/lang/NoClassDefFoundError: java/lang/ref/FinalReference
EDIT: NM figured it out.Last edited by OberSchuze; 01-16-2013 at 04:39 PM. Reason: fixed issue.
- 01-16-2013, 05:31 PM #4
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Re: Random selection
Here is my final code generation it works great. Now on to using the time variable to power the random number trigger time.
Java Code:public class randomgen { private static void doRawRandomNumber() { int rawRandomNumberCB; int mincb = 1; int maxcb = 18; int rawRandomNumberT; int mint = 6; int maxt = 30; for (int i = 0; i < 500; i++) { rawRandomNumberCB = (int) (Math.random() * (maxcb - mincb + 1) ) + mincb; rawRandomNumberT = (int) (Math.random() * (maxt - mint + 1) ) + mint; System.out.printf("Random Time and Circuit Breaker:\n" + "Circuit Breaker: " + rawRandomNumberCB +"\n" + "Time: " + rawRandomNumberT + "\n"); } } /** * Sole entry point to the class and application. * @param args Array of String arguments. */ public static void main(String[] args) { doRawRandomNumber(); } }Last edited by OberSchuze; 01-16-2013 at 05:41 PM. Reason: fixed a code error
- 01-16-2013, 08:21 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 6
- Rep Power
- 0
Re: Random selection
I am now trying to add a timer function to my file here so that after the 5th number set is generated it stops. I am not sure what I am doing wrong as what I have written has not tossed up any errors. Any help would be appreciated.
Java Code:import java.util.TimerTask; public class randomgen { class MyTask extends TimerTask{ private int times = 0; public void run() { times++; if (times <=5){ } else { this.cancel(); } } } private static void doRawRandomNumber() { int rawRandomNumberCB; int mincb = 1; int maxcb = 18; int rawRandomNumberT; int mint = 6; int maxt = 30; for (int i = 0; i < 500; i++) { rawRandomNumberT = (int) (Math.random() * (maxt - mint + 1) ) + mint; try{ Thread.sleep(rawRandomNumberT * 1000); } catch (InterruptedException e) {} rawRandomNumberCB = (int) (Math.random() * (maxcb - mincb + 1) ) + mincb; System.out.printf("Random Time and Circuit Breaker:\n" + "Circuit Breaker: " + rawRandomNumberCB +"\n" + "Time: " + rawRandomNumberT + "\n"); } } /** * Sole entry point to the class and application. * @param args Array of String arguments. */ MyTask t = new MyTask(); public static void main(String[] args) { doRawRandomNumber(); } }
Similar Threads
-
Using java.util.Random to choose a random sentence and print it
By skylerrivera17 in forum New To JavaReplies: 0Last Post: 01-23-2012, 09:12 AM -
Random selection of words
By netspinner in forum New To JavaReplies: 3Last Post: 12-14-2010, 05:31 PM -
how i use the random class to random the cards i have
By yanipao in forum New To JavaReplies: 14Last Post: 10-19-2009, 10:57 AM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks