java add items to arraylist at random time interva

2019-07-13 01:21发布

问题:

trying to add an item at random time intervals.

I was thinking I need to start when i is equal to arriveTime, once that is met I need to create a new random arrival and add i (otherwise it will not happen again as i is already past arrival. So I add another if, once that is met create new arrival time and add i again. pseudocode seems to make sense, code not so much, any help is appreciated.

    ArrayList<Integer> q = new ArrayList<Integer>();
    Random r = new Random();
    int arrivals = 0;

    for (int i = 1; i <= 720; i++) {
        int arrive = r.nextInt(4)+1;
        if (i == arrive) {
            q.add(i);
            arrivals ++;
            arrive = r.nextInt(4)+1+i;
        }
        else if (i == arrive) {
            q.add(i);
            arrivals ++;
            arrive = r.nextInt(4)+1+i;
        }

    }

sorry, arriveTime should be just arrive. ArriveTime does not exist.

edit: To expand from comments. 'i' represents time and I dont want to add a random integer to the list. Rather add the same object at random intervals of 'i'. I was adding the value of 'i' to the list to see at what times the algorithm was adding an item because it didnt seem to be working. Results vary, but it seems to be always single digits that get added to list. Also made updates to code.

回答1:

you should be comparing time to 1-4 in your if statements, then adding that value to i (as i is representative of time) Something like this

    ArrayList<Integer> q = new ArrayList<Integer>();
    Random r = new Random();
    int arrivals = 0;

    for (int i = 1; i < 100 ; i++) { //loop as many times as you want.
        int time = r.nextInt(4)+1; //choose a random time of arrival, 1-4

        //now compare time to your random time intervals (1-4, not i
        //if it matches add that value to i
        if (time == 1) {  
            //add your object to the list
            i += 1; 
        }
        else if (time == 2) {
            //add your object to the list
            i += 2;
        }

        else if (time == 3) {
            //add your object to the list
            i += 3;
        }
        else if (time == 4) {
            //add your object to the list
            i += 4;
        }
    }


回答2:

Your algorithm is lacking a pause - ie a call to Thread.sleep(), otherwise it will spin.

I'd be trying to keep it simple, matching your code to the problem: ie wait a random time between adding to the queue, simply:

ArrayList<Integer> q = new ArrayList<Integer>();
Random r = new Random();

for (int i = 1; i <= 720; i++) { // loop as many times as you want
    Thread.sleep(r.nextLong() % 1000); // wait a random time up to 1 second
    q.add(r.nextInt()); // add a random number to the queue
}

You can adjust the numbers to suit your requirements.



回答3:

The code as posted doesn't make much sense.

  • It depends on an undeclared variable.
  • The statements that update arrive have no effect ... because it it local to the loop.