BBladem83
BBladem83

Reputation: 623

Implementing Random Int in Java

I am using Blue J for reference.

What I need to do is after defining 2 constants, MIN = 60 and MAX = 180, which I already did, I am supposed to define the processtime as a random integer between 60 and 240, instead of the constant 120.

Problem is that I am unsure how to do that.

Here is the class, TicketCounter, in which that is supposed to be implemented in.

public class TicketCounter    
{
   final static int PROCESS = 120;
   final static int MAX_CASHIERS = 10;
   final static int NUM_CUSTOMERS = 200;
   final static int ARR_TIME = 20;
   final static int MIN = 60;
   final static int MAX = 180;

   public static void main ( String[] args) 
   {
  Customer customer;
  LinkedQueue<Customer> customerQueue = new LinkedQueue<Customer>();
  int[] cashierTime = new int[MAX_CASHIERS];    
  int totalTime, averageTime, departs;     

  System.out.printf("%-25.30s  %-30.30s%n", "Number of Cashiers", "Average Time (in seconds)");

  /** process the simulation for various number of cashiers */
  for (int cashiers=0; cashiers < MAX_CASHIERS; cashiers++)
  { 
     /** set each cashiers time to zero initially*/
     for (int count=0; count < cashiers; count++)
        cashierTime[count] = 0;

     /** load customer queue */
     for (int count=1; count <= NUM_CUSTOMERS; count++)
        customerQueue.enqueue(new Customer(count*ARR_TIME));

     totalTime = 0;

     /** process all customers in the queue */
     while (!(customerQueue.isEmpty())) 
     {
        for (int count=0; count <= cashiers; count++)
        {
           if (!(customerQueue.isEmpty()))
           {
              customer = customerQueue.dequeue();
              if (customer.getArrivalTime() > cashierTime[count])
                 departs = customer.getArrivalTime() + PROCESS;
              else
                 departs = cashierTime[count] + PROCESS; 
              customer.setDepartureTime (departs);
              cashierTime[count] = departs;
              totalTime += customer.totalTime();
           }
        }
     }

     /** output results for this simulation */
     averageTime = totalTime / NUM_CUSTOMERS;
     System.out.printf("%10s  %30s%n", (cashiers+1), averageTime);
      }
   }
}

Thank you in advance!

Upvotes: 0

Views: 428

Answers (2)

Olindholm
Olindholm

Reputation: 390

I'm not fully sure if I understand what you're looking for, but what I am certain of is that you want to randomize an integer. This can easily be done by the Random class.

What I'm not sure about is whether you want to randomize a number between 60-120 (MIN-MAX) or 60-280. Whether the case it should look something like this.

Random r = new Random();
int randomInt = r.nextInt(MAX - MIN + 1) + MIN;

This code will now result the variable to be an integer between MIN and MAX, now you can easily replace those with constants or set values to them for nice and understandable code.

For further information of what the code exactly does, I would recommend you again, to take a look at the Random class, I find it very useful in many cases.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201517

If you have MIN and MAX, and can use Random you might do -

Random random = new Random();
int MIN = 60;
int MAX = 180;
int val = random.nextInt(MAX + 1) + MIN;

Which will generate a random value between 60 inclusive and 240 inclusive. Random.nextInt(int) which (per the Javadoc),

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive).

Upvotes: 0

Related Questions