Results 1 to 7 of 7
Thread: Using the random class
- 04-26-2010, 01:58 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Using the random class
For this method, i have to randomly generate a new job with random priority and execution time. The chances of creating a new job is equal to PERCENT_NEW_JOB which is 6. Im not sure on how to create a new job with a random chance of actually creating. heres the code
/*Randomly generates a new job with random priority and execution time.
* The chances of creating a new job is equal to PERCENT_NEW_JOB.
* after the creation the job is placed in the waitingJobs queue.
*/
public static void generateJobs(int clock)
{
int randomPriority = Math.abs(generator.nextInt()) % 4 + 1;
int randomExecutionTime = Math.abs(generator.nextInt()) % 10 + 1;
Job job = new Job(randomPriority, randomExecutionTime, clock);
waitingJobs.offer(job);
}
- 04-26-2010, 02:54 AM #2
Hi There schmitty
first of all its a good idea to use code tags when posting code
here is how Java Forums - BB Code List
so it now looks like this
but perhaps your question isnt phrased properlyJava Code:/* * Randomly generates a new job with random priority and execution time. The * chances of creating a new job is equal to PERCENT_NEW_JOB. after the * creation the job is placed in the waitingJobs queue. */ public static void generateJobs(int clock) { int randomPriority = Math.abs(generator.nextInt()) % 4 + 1; int randomExecutionTime = Math.abs(generator.nextInt()) % 10 + 1; Job job = new Job(randomPriority, randomExecutionTime, clock); waitingJobs.offer(job); }
it looks to my newbie eyes like you have sucessfully created a new Job
when you say
do you mean that you want to randomly create the Job (sometimes you will sometimes you wont ) that has just been randomly initializedIm not sure on how to create a new job with a random chance of actually creating.
orare you just trying to randomly create the variables that make up the Job..
sorry if this doesnt make much sense but im also a noob,
it helps to be clear with your questions How To Ask Questions The Smart Way
Kind regards
Sonny:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 04-26-2010, 03:05 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
yea i want to randomly create the job as in sometimes it will work and sometimes it wont
- 04-26-2010, 03:28 AM #4
okay but i am guessing a bit here
i am assuming that that you already have something like this in your class
alreadyJava Code:private RandomGenerator generator = new RandomGenerator();
if that is the case you can use the next boolean method
something like
double p represents the probability,Java Code:if(generator.nextBoolean(double p){ Job job = new Job(randomPriority, randomExecutionTime, clock); waitingJobs.offer(job); }
so for example if you wanted a 65% percent probability you would use
take a look at The acm.util.RandomGenerator Class for details on random generator classJava Code:if(generator.nextBoolean(0.65){ Job job = new Job(randomPriority, randomExecutionTime, clock); waitingJobs.offer(job); }
and best of luck hope this helps
Sonny:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 04-26-2010, 03:35 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
thanks alot sonny, i was looking up the random class but just couldnt figure it out, thanks again
-
RandomGenerator is not part of the standard Java library. Are you sure that you're supposed to be using this class? Myself, I'd use the standard Random class, call nextDouble() which gives a double between 0 and 1, and use an if (myRandomDouble > 0.65)... e.g.
etc...Java Code:Random random = new Random(); double myRandomDouble = random.nextDouble(); if (myRandomDouble > 0.65) { //.... do something }
- 04-26-2010, 03:56 AM #7
if you have this declared in your class
0.06 is 6%Java Code:private final double PERCENT_NEW_JOB = 0.06;
i think you should be able to do this
which would be much better. but dont trust me play with it, and post any problems,, OR mark this thread solved if you have sorted out the problemJava Code:if(generator.nextBoolean(PERCENT_NEW_JOB){ Job job = new Job(randomPriority, randomExecutionTime, clock); waitingJobs.offer(job); }
knid regards
Sonny:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
Similar Threads
-
Random class
By KM88 in forum New To JavaReplies: 9Last Post: 10-27-2009, 08:55 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 -
Help with class project, random number generator.
By Christopher The Great in forum New To JavaReplies: 4Last Post: 03-14-2009, 02:12 AM -
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