generate a random number from 100 to 150, inclusive in applet
im not sure how to code the generated a random number from 100 to 150 inclusive for an applet
Re: generate a random number from 100 to 150, inclusive in applet
What have you tried? What did google tell you?
Re: generate a random number from 100 to 150, inclusive in applet
in goggle i found
int Low = 10;
int High = 100;
int R = r.nextInt(High-Low) + Low;
Re: generate a random number from 100 to 150, inclusive in applet
but r.nextInt(High-Low) + Low i know isnt applet
Re: generate a random number from 100 to 150, inclusive in applet
...what?
Please see the link in my signature on asking smart questions.
Re: generate a random number from 100 to 150, inclusive in applet
Quote:
Originally Posted by
chonch
but r.nextInt(High-Low) + Low i know isnt applet
Lesson: Applets (The Java™ Tutorials > Deployment)
Code Java:
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
public class HelloWorld extends JApplet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JLabel lbl = new JLabel("Hello World");
add(lbl);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
Re: generate a random number from 100 to 150, inclusive in applet
if java.util.Random is available in applet environment then i don't see a problem. The thing is r.nextInt() where r is an instance of Random, produces negative value. So to produce a number between 100 and 150 inclusive u do is this:
Code java:
int a = r.nextInt() % 151 ;
a = a > 0 ? a : -1 * a ; // making it positive
if (a > 150){
a %= 150 ; // cutting down to size
if (a < 100)
a += 100 ;
if i misunderstood ur problem then sorry, very much sorry.
Re: generate a random number from 100 to 150, inclusive in applet
Quote:
Originally Posted by
dumb_terminal
if java.util.Random is available in applet environment then i don't see a problem. The thing is r.nextInt() where r is an instance of Random, produces negative value. So to produce a number between 100 and 150 inclusive u do is this:
Code java:
int a = r.nextInt() % 151 ;
a = a > 0 ? a : -1 * a ; // making it positive
if (a > 150){
a %= 150 ; // cutting down to size
if (a < 100)
a += 100 ;
if i misunderstood ur problem then sorry, very much sorry.
...are you serious?
Re: generate a random number from 100 to 150, inclusive in applet
Quote:
Originally Posted by
KevinWorkman
...are you serious?
Exactly what I thought :D
Re: generate a random number from 100 to 150, inclusive in applet
Quote:
Originally Posted by
dumb_terminal
Code java:
int a = r.nextInt() % 151 ;
a = a > 0 ? a : -1 * a ; // making it positive
if (a > 150){
a %= 150 ; // cutting down to size
if (a < 100)
a += 100 ;
Ugh, seems to me like this is all he needs:
Code java:
int a = r.nextInt() % 51 + 100;
Re: generate a random number from 100 to 150, inclusive in applet
Quote:
Originally Posted by
Gerp
Ugh, seems to me like this is all he needs:
Why use the modulus operator at all? You're a lot closer to being correct than dumb_terminal, but I don't see a need for modulus here at all.
Besides, spoonfeeding is NOT helpful to the OP. He's probably long gone by now anyway.