George Lin

Ranch Hand

Posts: 125

posted 12 years ago

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?

Thanks in advance,

George

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?

Thanks in advance,

George

posted 12 years ago

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

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 ]

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

Posts: 125

posted 12 years ago

Thanks marc,

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

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 shouldaverage60 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

posted 12 years ago

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

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

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

*~Joe Strummer*

sscce.org

George Lin

Ranch Hand

Posts: 125

posted 12 years ago

Thanks marc,

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

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

posted 12 years ago

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

You might start here:

http://en.wikipedia.org/wiki/Normal_distribution

*~Joe Strummer*

sscce.org

George Lin

Ranch Hand

Posts: 125

posted 12 years ago

Thanks marc,

It is very helpful!

regards,

George

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

It is very helpful!

regards,

George

Layne Lund

Ranch Hand

Posts: 3061

posted 12 years ago

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

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

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |