1. Member
Join Date
Jan 2010
Posts
14
Rep Power
0

## Math.random

Generating random value in range 3-18
Random randomNum = new Random ();
int variable1 = randomNum.nextInt(16) + 3;

might be helpful for sombody
Last edited by p0rnstar; 01-26-2010 at 01:18 PM.

2. Senior Member
Join Date
Jun 2008
Posts
2,563
Rep Power
8
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.

3. Member
Join Date
Jan 2010
Posts
14
Rep Power
0
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

4. 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!

5. *ahem*
Random (Java Platform SE 6)

psssst: click the link!

6. Member
Join Date
Jan 2010
Posts
8
Rep Power
0

call this method:nextInt(int a),for example,nextInt(16),will generate an integer from 0 to 15 randomly

7. You guys are all skirting around the issue, and yes, read the API!
This is so simple:
Java Code:
```Random ran = new Random(System.nanoTime());
System.out.println(ran.nextInt(16)+3);```

8. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
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.

9. Senior Member
Join Date
Jun 2008
Posts
2,563
Rep Power
8
Originally Posted by quad64bit
You guys are all skirting around the issue, and yes, read the API!
This is so simple:
Java 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.

10. 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•