Jump to content

A question about the Random class

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Joe N

Joe N

    Newbie

  • Members
  • PipPip
  • 14 posts
Good morning fellow programmers,

I've been messing around for the last few hours or so trying to utilize the Random class to no avail :confused:. What I'm trying to do is write a method that will call nextInt() after the previous run of it has selected a number. Let me show you in code:



Random generator = new Random();


static void genNumb() {


generator.nextInt();


}


int events = 0;


while (events < 10) {


genNumb();


System.out.println("Something happened!");


}


if(events == 10) {


break;


}



But the problem I am encountering is nextInt() doesn't seem to be grabbing a number. It will just flood the output all at once with "Something happened!". What I want to happen is an event will happen after genNumb has selected a number and then print out "Something happened". I don't want it to happen all at once, I want it to happen randomly (hence the reason I'm using random).

I've been up for 17 hours so I apologize if my intentions aren't very clear. I'll be more than happy to specify additional information/clarification after I get some sleep :closedeyes:.

Thanks.

#2
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
You're not increasing events variable - it remains 0 and your while loop becomes an infinite loop. Add one events++ inside the loop and that'll do it.

#3
Joe N

Joe N

    Newbie

  • Members
  • PipPip
  • 14 posts
Yes, I realized I forgot to include that piece of code. However, I have been doing that in my test all along. What is happening is that when it reaches the nextInt part, it waits a certain amount of time (probably a few seconds) and then outputs the text and adds to events until 10 have been reached. I do not want it to do it all at once - I want it to be delayed IE two minutes go by then "Something happened!", one second goes by "Something happened!", etc. - completely random. I can post code if needed.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
You probably want
try{
  Thread.sleep( genNumb() )
}catch(InterruptedException IE){
  //error occured, normally won't happen
}
your genNumb() will however sometimes generate very big numbers so it will sleep for a looong time. Even if the sleep parameter is in milliseconds.
That's why you should give a maximum value:
generator.nextInt(120000);


#5
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Try using Thread.sleep function. It receives amount of time (in milliseconds) it should block thread execution. You can use value returned by nextInt() to calculate amount of time to sleep, which would make a random behaviour.

Also, do not use nextInt() but rather nextInt(n), where n is upper exclusive bound of numbers returned by the function.

#6
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts

oxano said:

You probably want...
You were faster by two minutes, older twin :)

#7
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
import java.util.Random;

public class Demo{
    public static void main(String[] args) {
        WaitRandomly w = new WaitRandomly();
        w.run(10);
    }    
}


class WaitRandomly{
    public static final int MAX_WAIT = 3000;
    private Random generator = new Random();

    private int genNumb() {
        return generator.nextInt(MAX_WAIT);
    }
    
    public void run(int times) {
        for(int events = 0; events < times; events++) {            
            try {
                Thread.sleep(genNumb());
            } catch (InterruptedException e) {                
                e.printStackTrace();
            }             
            System.out.println("Something happened!");
        }    
    }
}
something like this?

#8
Joe N

Joe N

    Newbie

  • Members
  • PipPip
  • 14 posts
Thank you all for your input. The try and Thread.sleep did the trick, my program is working how I want it to now :thumbup:.

Thanks again :w00t: