# Math.random

Printable View

• 01-22-2010, 08:22 AM
p0rnstar
Math.random
Generating random value in range 3-18
Random randomNum = new Random ();
int variable1 = randomNum.nextInt(16) + 3;

might be helpful for sombody
• 01-22-2010, 08:56 AM
masijade
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.
• 01-26-2010, 01:54 AM
p0rnstar
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
• 01-26-2010, 01:59 AM
Fubarable
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!
• 01-26-2010, 02:18 AM
Fubarable
*ahem*
Random (Java Platform SE 6)

psssst: click the link!
• 01-26-2010, 08:16 AM
852000
hope help you
call this method:nextInt(int a),for example,nextInt(16),will generate an integer from 0 to 15 randomly
• 01-26-2010, 09:27 AM
quad64bit
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);```
• 01-26-2010, 02:04 PM
Tolls
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.
• 01-26-2010, 11:55 PM
masijade
Quote:

Originally Posted by quad64bit
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);```

I really don't know how you consider this "skirting around the issue". It is called "forcing the OP to use the tools that he will need to use in order to be a developer". I consider that very beneficial.
• 01-27-2010, 02:26 AM
quad64bit
Quote:

Originally Posted by masijade
I really don't know how you consider this "skirting around the issue". It is called "forcing the OP to use the tools that he will need to use in order to be a developer". I consider that very beneficial.

You're right of course. Guess I was having a long day, shoulda kept my mouth shut :D