Generating random value in range 3-18
Random randomNum = new Random ();
int variable1 = randomNum.nextInt(16) + 3;
might be helpful for sombody
Printable View
Generating random value in range 3-18
Random randomNum = new Random ();
int variable1 = randomNum.nextInt(16) + 3;
might be helpful for sombody
3-18 is the same as 0 to 15 + 3, right? So take another look at the API docs for Random and see if there is another method there that will let you restrict your nextInt to max 15.
So the range or random number is 3-18
for a while i wrote something like that
Random rnd = new Random ();
int Strength = 3+rnd.nextInt() 16;
int Intelligence = 3+rnd.nextInt() % 16;
(... and so on)
The problem is that it gives my negative variable. i need just positive in range 3 - 18
To repeat what was stated above: look at the API docs for Random and see if there is another method there that will let you restrict your nextInt to max 15.
Much luck!
*ahem*
Random (Java Platform SE 6)
psssst: click the link!
call this method:nextInt(int a),for example,nextInt(16),will generate an integer from 0 to 15 randomly
You guys are all skirting around the issue, and yes, read the API!
This is so simple:
Code:Random ran = new Random(System.nanoTime());
System.out.println(ran.nextInt(16)+3);
By the way, if you're planning on using this to represent 3d6, it won't give you the correct distribution. You'll need 3 calls to nextInt(), each being a 1-6 range, then add them together. Just saying, because of your other thread on some RPG thing you're doing.