How to print out string at random interval

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

How to print out string at random interval

 
0
  #1
Apr 7th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: How to print out string at random interval

 
0
  #2
Apr 8th, 2005
I tried this out and here's the method I came up with... enjoy!

  1. public class Rand
  2. {
  3. public static void main(String[] args)
  4. {
  5. String[] sa = {"HelloWorld", " "}; // holds Hello World or just a blank string
  6. for (int i=0; i<60000; i++) // i think this loop takes aboout a minute
  7. { // but im not sure
  8. int num = (int) (Math.random() * 2); // chooses a random number (0 or 1)
  9. System.out.println(sa[num]); // and then prints out the corresponding String
  10. // from above
  11. }
  12. }
  13. }

Nick Nisi
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: How to print out string at random interval

 
0
  #3
Apr 8th, 2005
it isnot very useful, since the loop can vary from CPU to CPU. Faster CPUs process the loop quicker...
another approch can be with creating a thread(timertask) and a timer class, check out API docs for more....
Last edited by tonakai; Apr 8th, 2005 at 3:14 am. Reason: add (timertask)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: How to print out string at random interval

 
0
  #4
Apr 8th, 2005
Thanks tonakai,


Originally Posted by tonakai
it isnot very useful, since the loop can vary from CPU to CPU. Faster CPUs process the loop quicker...
another approch can be with creating a thread(timertask) and a timer class, check out API docs for more....
Your reply is very helpful. Can you describe your idea of "another approch can be with creating a thread(timertask) and a timer class" in more details?


regards,
George
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 189
Reputation: George2 is an unknown quantity at this point 
Solved Threads: 0
George2 George2 is offline Offline
Junior Poster

Re: How to print out string at random interval

 
0
  #5
Apr 8th, 2005
Thanks stupidenator,


Originally Posted by stupidenator
I tried this out and here's the method I came up with... enjoy!

  1. public class Rand
  2. {
  3. public static void main(String[] args)
  4. {
  5. String[] sa = {"HelloWorld", " "}; // holds Hello World or just a blank string
  6. for (int i=0; i<60000; i++) // i think this loop takes aboout a minute
  7. { // but im not sure
  8. int num = (int) (Math.random() * 2); // chooses a random number (0 or 1)
  9. System.out.println(sa[num]); // and then prints out the corresponding String
  10. // from above
  11. }
  12. }
  13. }

Nick Nisi
How can you draw the conclusion that the loop in your sample will takes about a minute?


regards,
George
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: How to print out string at random interval

 
0
  #6
Apr 8th, 2005
Here an example,
myTimer.java
  1. import java.util.*;
  2.  
  3. public class myTimertask extends TimerTask {
  4. public void run() {
  5. System.out.println("Hello World!");
  6. }
  7. }

myTimertaskUser.java
  1. import java.util.*;
  2. public class myTimerUser {
  3. public static void main(String[] args) {
  4. myTimertask testTimertask = new myTimertask();
  5. Timer testTimer = new Timer();
  6. testTimer.schedule(testTimertask,1000,1000);
  7. }
  8. }

well after i write this, i see that it is not possible to change its display time directly, so you need to figure out some other way, like after displaying text, cancel the timer, then generate a new random number, then start again.... i can give you another example but, i am a bit lazy....
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How to print out string at random interval

 
0
  #7
Apr 8th, 2005
for (int i=0; i<60000; i++) // i think this loop takes aboout a minute
It won't On my machine at work (which is extremely slow because of lack of memory, it often spends 5 minutes swapping data when I switch applications) it took 2 minutes almost to the second.

Anyway, it won't print out strings with an average interval of one minute.
For that you need to use a Thread which sleeps for a random interval.
Of course you can't possibly create an unknown number of random intervals yet have those have an average time of a minute.
At best you can create a random number between 2 limits and use that as the interval where the average between those numbers is one minute (in milliseconds which is the measure of time used by Thread.sleep()).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: How to print out string at random interval

 
0
  #8
Apr 8th, 2005
I'm not really sure why I thought that loop would last for a minute. Sorry about that...

Nick Nisi
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: How to print out string at random interval

 
0
  #9
Apr 8th, 2005
to average one minute, I would use java.util.Random and do the following...

  1. import java.util.Random;
  2.  
  3. class OneMinute
  4. {
  5. public static void main(String[] args)
  6. {
  7. double numSeconds, numSecondsTotal;
  8. Random random = new Random();
  9.  
  10. numSecondsTotal = 0;
  11. for (int i=0; i<100; i++)
  12. {
  13. numSeconds = (random.nextFloat()+0.5)*60;
  14. numSecondsTotal += numSeconds;
  15. System.out.println(numSeconds);
  16. }
  17. System.out.println("The average number of seconds is: "+ numSecondsTotal/100);
  18.  
  19. }
  20. }

I tried usign nextGaussian() but there is no way to change the standard distrubution to something else, but anyways this should work, i tried it out and it always averaged between 58 - 61 seconds, but has a range of 30-90 seconds (aka 0.5 - 1.5 minutes). You can tweak with it a bit to get a bigger range (or smaller) if need be. Use this in conjuction with tonakai's post to get what u need.
http://java.brangle.com/tutorials/java/util/Random.php
Last edited by paradox814; Apr 8th, 2005 at 5:46 pm. Reason: removed unneeded code
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Gaussian method for much bigger range

 
0
  #10
Apr 8th, 2005
Ok so I got bored and decided to do the gaussian method, of course I kinda forced it to do what i wanted with the if statement in there, but either way this has a much much bigger range of 0 - 120 seconds
  1. import java.util.Random;
  2.  
  3. class OneMinute
  4. {
  5. public static void main(String[] args)
  6. {
  7. double numSecondsTotal;
  8. double i=0, numSeconds =0;
  9. Random random = new Random();
  10.  
  11. numSecondsTotal = 0;
  12.  
  13. do
  14. {
  15. numSeconds = (random.nextGaussian()+1)*60.0;
  16. if (numSeconds >= 0.0 && numSeconds <= 120.0)
  17. {
  18. i++;
  19. numSecondsTotal += numSeconds;
  20. System.out.println(numSeconds);
  21. }
  22. }
  23. while (i < 100);
  24. System.out.println("The average number of seconds is: "+ numSecondsTotal/100);
  25.  
  26. }
  27. }

http://java.brangle.com/tutorials/java/util/Random.php
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 6765 | Replies: 20
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC