• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to print out string at random interval

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



It is very helpful!


regards,
George
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Shiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic