2

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.

5
  • What exactly is your question? Are you having compile errors, wrong results, etc? It would be helpful if you could also add your expected output of the code and what you are getting currently. Commented Jul 10, 2011 at 4:55
  • @cool beans: it only adds a few items. Results vary as items are to be added at random intervals. No errors. It seems like it should add more than just 3-4 items to list. Commented Jul 10, 2011 at 4:59
  • @Minh - what's the value of arriveTime? Commented Jul 10, 2011 at 5:11
  • The code as you've presented it is missing the declaration for arriveTime. Please post the real code. Commented Jul 10, 2011 at 5:16
  • @CoolBeans @Stephen C. sorry, arriveTime should be just arrive. ArriveTime does not exist. I copied code wrong. this doesnt seem to work Commented Jul 10, 2011 at 5:26

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Dont understand, '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.
Well, I couldn't understand your question - I based it on the title. Could be more specific about what it is you're trying to achieve - I'll try to help you.
0

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

Comments

0

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.

1 Comment

sorry, arriveTime should be just arrive. ArriveTime does not exist. I copy code wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.