Java Programming Tutorial – 26 – Random Number Generator
How to use a random number generator in java programming.
May 2, 2010 | Filed Under Java Tutorials
Comments
25 Responses to “Java Programming Tutorial – 26 – Random Number Generator”
Powered by Yahoo! Answers
can you do something like… for my FRP (DND) game i want to put like health from 10 to 100 Dmg from 5 to 20 Armor 1 to 15 and and random it up?
System.out.println(“Thanx”);
Of course, just take the difference and add the minimum #
ok I will try it…
thank you :)
I prefer to use Math.random() so I dont have to import anything.
explanation:
Math.random() outputs a decimal number between 0 and 1 (0.1, 0.5, 0,7) I multiply that number with 6 = (0.6*6 = 3.6 , 0.2 * 6 = 1.2 etc.) then I add 1 and cast that into an int: So the whole thing will be: (int)(Math.random() * 6 + 1);
What do you people think of this method? is it long-winded or..?
Saucebiotch, I know it’s been 3 weeks now since you posted this comment but for the 2nd problem is this a possibility: If(inputString.equalsIgnoreCase(“quit”) {
//break loop..
}else {
//proceed
}
what pempek8282 said…enough said…
That’s still good but it’s not pseudo-random.
for more information abou random i recommend u to seee
java(dot)sun(dot)com(slash)j2se(slash)1.4.2(slash)docs(slash)api(slash)java(slash)util(slash)Random(dot)html
and for any query about any thing in java i welcome u all to add me at my skypeid/google talk id=hmk25061984
like random generator i have random behaviour , is there is any normal generator ..)
what compiler is he using?
@Hellfire928 Look at the earliest tutorials, i think around 1-5, there he tells what he uses and so on..
@SirStiller
Addition is commutative so it wouldn’t really make a difference
@ultimatedorkwad
This would work but surely it’s easier to just add the 1 rather than typing a whole if statement?
@Hellfire928
The compiler built in the Eclipse IDE :)
Good vid, Bucky! I only knew of the Math.random way until now.
Thanks so much
bucky is using eclipse
If i do this prigramm with a while loop it puts only 1 number out?!
Can anyone tell me y?
we dont want bucky to die!!!
Good tutorial, but say you wanted to take that random number and add or divide it to another random number and print the result?? for example, 1 + 3 = 4, or 4 / 2 = 2.. How would you do it ? And to add, how would you place those random number if need be in a range, e.g. numbers random numbers between 1 and 10, or random numbers between 10 and 20 ?
@Hauserdav
Need to have your condition set in there
while(condition/arg)
for example while(true)
while(variable>=10)
just play with it :P
You Are amazing… Thanks a Lot!!
@nthenameof HEy, i was wondering that too… did u end up with a solution??/
random tip for all viewers: if you don’t want to keep typing “System.out.println(” “);”
just do:
(The numbers next to the brackets are to show relate them)
class example{1
public static void main(String args[]){2
}2
private static void log(String aMessage){3
System.out.println(aMessage);
}3
}1
just type that within your class brackets and outside of your public static brackets
now instead of typing System.out.println(” “); you type: log(” “);
tnongs
@2plus2equalswhat or you cud write syso and then hold CTRL click space and release both
Radom is actually a city in Poland :) made me laugh