See this thread about java.util.Random:
URL
The gist of it being that you need to instantiate the Random object just once (say as a class variable of you applet or something), not each time the update display method is called, due to the nature of how the Random object spits out numbers.
i.e.:
Code:
class foo {
Random rand = new Random(optional seed here);
(array of strings for shape tokens)
(blah blah code)
(update method) {
int array_index = rand.nextInt(tie-in to shape array's length);
blah blah
}
(yet more code)
}
Come to think of it you don't need the minus one on the array's length when giving the arg to nextInt, rereading the docs it seems that the method returns an int from 0 to one less than the arg you give (i.e. it's inclusive of zero but exclusive of the arg, naturally you'll want to test this), which is exactly the behavior you want to get valid array indexes.
Here's a simple little class that prints out 40 random integers from 0 to 10 using the methods described above:
Code:
import java.util.Random;
public class test {
static Random rand = new Random();
public static void main(String[] args) {
int i;
for (i = 0; i < 40; i++) {
System.out.println(rand.nextInt(11));
}
}
}
(naturally, you'll have to stick that into a file called test.java to compile and test it... and it does presume the existence of a command line (dos, unix, whatever))
One advantage to java.util.Random is that it's been around since JDK 1.0 so it should run on just about any browser jvm out there...