I want to print out the string "Hello World!" to console at a random interval, and the average length of the random interval is 1 minute when the string is printed out hundreds of times. Anyone know how to accomplish this?

A call to Thread.sleep(long milliseconds) is the easiest way to create "pauses."

To me, the question is about these random intervals. I understand they should average 60 seconds (60000 milliseconds), but what should their range and distribution be?

If you're okay with a uniform distribution, then you can use Math.random(). For example, if you wanted the values to range between 50 and 70 seconds (a span of 20 seconds), you could use something like...

int secs = (int)(Math.random() * 20) + 50;

But if you need to work with a normal ("bell curve") distribution, then you might look at the nextGaussian() method in the java.util.Random class. [ April 07, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org

George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125

posted

0

Thanks marc,

Originally posted by marc weber: A call to Thread.sleep(long milliseconds) is the easiest way to create "pauses."

To me, the question is about these random intervals. I understand they should average 60 seconds (60000 milliseconds), but what should their range and distribution be?

If you're okay with a uniform distribution, then you can use Math.random(). For example, if you wanted the values to range between 50 and 70 seconds (a span of 20 seconds), you could use something like...

int secs = (int)(Math.random() * 20) + 50;

But if you need to work with a normal ("bell curve") distribution, then you might look at the nextGaussian() method in the java.util.Random class.

[ April 07, 2005: Message edited by: marc weber ]

Your reply is very helpful and I think you are an expert of this field. I want to learn more about how to use Java to generate more complex random intervals, for example, different range and distributions, how to use a normal ("bell curve") distribution and nextGaussian() method. Can you recommend me some online resources?

Originally posted by George Lin: ...I think you are an expert of this field...

Thank you, but I am not an expert.

The API for java.util.Random indicates that nextGaussian() returns a pseudorandom double from a normal distribution with mean 0.0 and standard deviation 1.0.

So to refine the variance, I expect that you would multiply the returned value by a new standard deviation. Then, to refine the mean, you would simply add the new mean.

For example, if you wanted random numbers from a normal distribution with a mean of 45.67 and a standard deviation of 3.4, you would use something like...

Random rGen = new Random(); double normRand = (rGen.nextGaussian() * 3.4) + 45.67;

I put the above code in a small test program, and the output looks right. But this is actually the first time I've ever used nextGaussian(), so perhaps a real expert can confirm...?

The API for java.util.Random indicates that nextGaussian() returns a pseudorandom double from a normal distribution with mean 0.0 and standard deviation 1.0.

So to refine the variance, I expect that you would multiply the returned value by a new standard deviation. Then, to refine the mean, you would simply add the new mean.

For example, if you wanted random numbers from a normal distribution with a mean of 45.67 and a standard deviation of 3.4, you would use something like...

Random rGen = new Random(); double normRand = (rGen.nextGaussian() * 3.4) + 45.67;

I put the above code in a small test program, and the output looks right. But this is actually the first time I've ever used nextGaussian(), so perhaps a real expert can confirm...?

Your reply is very helpful. I want to learn some basic knowledge of Gaussian algorithm (for example, mean and deviation). Can you recommend some online resources?

If you search the internet for "normal distribution," "standard deviation," etc. I'm sure you'll find plenty of material. Much of it will involve Calculus, but you should be able to find some less-intensive material as well.

Originally posted by marc weber: If you search the internet for "normal distribution," "standard deviation," etc. I'm sure you'll find plenty of material. Much of it will involve Calculus, but you should be able to find some less-intensive material as well.

I'd like to point out that most of the discussion here is not specific to Java or even computer programming. Probability distributions, mean, standard deviation, etc. are all topics from statistics. If you are in college, you should take a course to learn more than you ever want to know about this topic.

However, when it comes to figuring out how things are specifically implemented in Java, you should always consult the Java API documentation for the version of Java you are using. With some experience navigating these docs, you can find the answers to many of your questions on your own.