Results 1 to 7 of 7
  1. #1
    schmitty514 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default 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);
    }

  2. #2
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default 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
    Java 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);
    	}
    but perhaps your question isnt phrased properly

    it looks to my newbie eyes like you have sucessfully created a new Job

    when you say
    Im not sure on how to create a new job with a random chance of actually creating.
    do you mean that you want to randomly create the Job (sometimes you will sometimes you wont ) that has just been randomly initialized
    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

  3. #3
    schmitty514 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    yea i want to randomly create the job as in sometimes it will work and sometimes it wont

  4. #4
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    okay but i am guessing a bit here
    i am assuming that that you already have something like this in your class

    Java Code:
    private RandomGenerator generator = new RandomGenerator();
    already

    if that is the case you can use the next boolean method

    something like
    Java Code:
    if(generator.nextBoolean(double p){
    			Job job = new Job(randomPriority, randomExecutionTime, clock);
    			waitingJobs.offer(job);
    		}
    double p represents the probability,
    so for example if you wanted a 65% percent probability you would use
    Java Code:
    if(generator.nextBoolean(0.65){
    			Job job = new Job(randomPriority, randomExecutionTime, clock);
    			waitingJobs.offer(job);
    		}
    take a look at The acm.util.RandomGenerator Class for details on random generator class

    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

  5. #5
    schmitty514 is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    thanks alot sonny, i was looking up the random class but just couldnt figure it out, thanks again

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.
    Java Code:
    Random random = new Random();
    
    double myRandomDouble = random.nextDouble();
    if (myRandomDouble > 0.65) {
      //.... do something
    }
    etc...

  7. #7
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    if you have this declared in your class
    Java Code:
    private final double PERCENT_NEW_JOB = 0.06;
    0.06 is 6%

    i think you should be able to do this
    Java Code:
    if(generator.nextBoolean(PERCENT_NEW_JOB){
    			Job job = new Job(randomPriority, randomExecutionTime, clock);
    			waitingJobs.offer(job);
    		}
    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 problem
    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

  1. Random class
    By KM88 in forum New To Java
    Replies: 9
    Last Post: 10-27-2009, 08:55 PM
  2. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  3. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  4. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 02:12 AM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •