Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# How to print out string at random interval

George Lin
Ranch Hand
Posts: 125
Hello everyone,

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?

George

marc weber
Sheriff
Posts: 11343
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 ]

George Lin
Ranch Hand
Posts: 125
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?

regards,
George

marc weber
Sheriff
Posts: 11343
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...?

Ref: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html

George Lin
Ranch Hand
Posts: 125
Thanks marc,

Originally posted by marc weber:

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...?

Ref: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html

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?

regards,
George

marc weber
Sheriff
Posts: 11343
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.

You might start here:
http://en.wikipedia.org/wiki/Normal_distribution

George Lin
Ranch Hand
Posts: 125
Thanks marc,

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.

You might start here:
http://en.wikipedia.org/wiki/Normal_distribution

regards,
George

Layne Lund
Ranch Hand
Posts: 3061
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.

HTH

Layne