| | |
How to print out string at random interval
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I tried this out and here's the method I came up with... enjoy!
Nick Nisi
Java Syntax (Toggle Plain Text)
public class Rand { public static void main(String[] args) { String[] sa = {"HelloWorld", " "}; // holds Hello World or just a blank string for (int i=0; i<60000; i++) // i think this loop takes aboout a minute { // but im not sure int num = (int) (Math.random() * 2); // chooses a random number (0 or 1) System.out.println(sa[num]); // and then prints out the corresponding String // from above } } }
Nick Nisi
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Solved Threads: 0
Thanks tonakai,
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
•
•
•
•
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....
regards,
George
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Solved Threads: 0
Thanks stupidenator,
How can you draw the conclusion that the loop in your sample will takes about a minute?
regards,
George
•
•
•
•
Originally Posted by stupidenator
I tried this out and here's the method I came up with... enjoy!
Java Syntax (Toggle Plain Text)
public class Rand { public static void main(String[] args) { String[] sa = {"HelloWorld", " "}; // holds Hello World or just a blank string for (int i=0; i<60000; i++) // i think this loop takes aboout a minute { // but im not sure int num = (int) (Math.random() * 2); // chooses a random number (0 or 1) System.out.println(sa[num]); // and then prints out the corresponding String // from above } } }
Nick Nisi
regards,
George
Here an example,
myTimer.java
myTimertaskUser.java
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....
myTimer.java
Java Syntax (Toggle Plain Text)
import java.util.*; public class myTimertask extends TimerTask { public void run() { System.out.println("Hello World!"); } }
myTimertaskUser.java
Java Syntax (Toggle Plain Text)
import java.util.*; public class myTimerUser { public static void main(String[] args) { myTimertask testTimertask = new myTimertask(); Timer testTimer = new Timer(); testTimer.schedule(testTimertask,1000,1000); } }
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....
•
•
•
•
for (int i=0; i<60000; i++) // i think this loop takes aboout a minute
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.
to average one minute, I would use java.util.Random and do the following...
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
Java Syntax (Toggle Plain Text)
import java.util.Random; class OneMinute { public static void main(String[] args) { double numSeconds, numSecondsTotal; Random random = new Random(); numSecondsTotal = 0; for (int i=0; i<100; i++) { numSeconds = (random.nextFloat()+0.5)*60; numSecondsTotal += numSeconds; System.out.println(numSeconds); } System.out.println("The average number of seconds is: "+ numSecondsTotal/100); } }
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
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
http://java.brangle.com/tutorials/java/util/Random.php
Java Syntax (Toggle Plain Text)
import java.util.Random; class OneMinute { public static void main(String[] args) { double numSecondsTotal; double i=0, numSeconds =0; Random random = new Random(); numSecondsTotal = 0; do { numSeconds = (random.nextGaussian()+1)*60.0; if (numSeconds >= 0.0 && numSeconds <= 120.0) { i++; numSecondsTotal += numSeconds; System.out.println(numSeconds); } } while (i < 100); System.out.println("The average number of seconds is: "+ numSecondsTotal/100); } }
http://java.brangle.com/tutorials/java/util/Random.php
![]() |
Other Threads in the Java Forum
- Previous Thread: Doubles
- Next Thread: Need Help with controling the speed of animation
Views: 6765 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database detection draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linux list loop map method methods mobile netbeans newbie number object objects oracle oriented os panel print problem program programming project projects recursion replaydirector reporting researchinmotion return robot scanner score screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows






