[java] Random numbers in JDK 1.1

Started by
6 comments, last by Viking 23 years, 9 months ago
Hello, I use random numbers in one of my applets I am currently developing. I am using Forte4Java and everthing works fine. But when it comes to open the file in the browser I fail to get it going. As far as I figured it out the line (I snipped the rest) int diceRoll = r.nextInt(maxNumber) + 1; is the problem. Anyone got an idea how to get around this? thanks in advance Juergen
Advertisement
1.1 doesn''t have any method

java.util.Random.nextInt(int n);

only

java.util.Random.nextInt();

Here''s how java 1.2 does it''s nextInt(int n) method


public int nextInt(int n) {
if (n<=0)
throw new IllegalArgumentException("n must be positive");

if ((n & -n) == n) // i.e., n is a power of 2
return (int)((n * (long)next(31)) >> 31);

int bits, val;
do {
bits = next(31);
val = bits % n;
} while(bits - val (n-1) < 0);
return val;
}

Get Jdk 1.2 or 1.3, you are wasting your time with 1.1

and if you are going to use the random method, don''t forget to import java.util.* unless you like to type
I''m not too awful sure which version this comes from, but the Math class has a psuedo random number generating function.. it''s just Math.random() If you are looking for a more sophisticated random number generation technique, like a guassian there is the Random class, where you can use
Random.setSeed(long seed); and
Random.nextGaussian();

But like I said I''m not sure which version of the java sdk supports this.

War doesn't determine who is right, war determines who is left.
Thanks for your help. I know I am wasting my time with JDK 1.1 and I already code in JDK 1.3. But as I mentioned earlier I am about to write an applet and AFAIK Netscape 4.7 and IE 5.01 only support JDK 1.1 (well to be exact: the JVM only supports JDK 1.1)

I downloaded and tried the Mozilla browser and the JVM update for IE and now it works on both browsers. BUT I have to ask: Do I really want users of my applet have to download plug-ins or new browsers just to get it going ?

I have no idea when I will be finished with the applet - maybe the JVM''s of the browsers support JDK1.2+ until then :-)

thanks again for your help

Juergen
1.3 comes with the Java Plugin so you can run Java2 classes in a standard browser. Get the HTML converter to change your web pages to use the plugin.

http://java.sun.com/products/plugin/1.3/features.html

IMHO, coding in 1.1 is not a waste of time if you need to run applets without plugin. It is a deployment issue. A pluggable design that runs under 1.1 or Java2, but takes advantage of Java2 features if it is present may be a good move for you. (The beauty of dynamic class loading...)

ManaSink
I have to agree with ManaSink, JDK 1.1 definitly isn''t a waste of time. IMO most people don''t want to download a plugin and you can still achieve the same effects with 1.1 as you can with most of the other SDK''s it just depends on how much time you want to spend writing compatible code. ^_^ my $.02

War doesn't determine who is right, war determines who is left.
And if they guy that signs your checks uses a Mac, you can''t very well tell him to "go get the plugin" when there isn''t a 1.2 plugin for macs. Well, you can, but that usually results in you not getting paid.

This topic is closed to new replies.

Advertisement