943,964 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 10494
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 7th, 2005
0

How to print out string at random interval

Expand Post »
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
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Apr 8th, 2005
0

Re: How to print out string at random interval

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

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Apr 8th, 2005
0

Re: How to print out string at random interval

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)
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Apr 8th, 2005
0

Re: How to print out string at random interval

Thanks tonakai,


Quote 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
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Apr 8th, 2005
0

Re: How to print out string at random interval

Thanks stupidenator,


Quote originally posted by stupidenator ...
I tried this out and here's the method I came up with... enjoy!

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 0
Junior Poster
George2 is offline Offline
189 posts
since Nov 2004
Apr 8th, 2005
0

Re: How to print out string at random interval

Here an example,
myTimer.java
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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....
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Apr 8th, 2005
0

Re: How to print out string at random interval

Quote ...
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()).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Apr 8th, 2005
0

Re: How to print out string at random interval

I'm not really sure why I thought that loop would last for a minute. Sorry about that...

Nick Nisi
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Apr 8th, 2005
0

Re: How to print out string at random interval

to average one minute, I would use java.util.Random and do the following...

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Apr 8th, 2005
0

Gaussian method for much bigger range

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
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Doubles
Next Thread in Java Forum Timeline: Need Help with controling the speed of animation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC