Results 1 to 10 of 10
Thread: Math.random
- 01-22-2010, 07:22 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
- 01-22-2010, 07:56 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- 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.
- 01-26-2010, 12:54 AM #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
-
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!
- 01-26-2010, 07:16 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
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, 08:27 AM #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);
- 01-26-2010, 01:04 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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, 10:55 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
- 01-27-2010, 01:26 AM #10
Similar Threads
-
use Math.random and then count occurrences
By iliak67 in forum New To JavaReplies: 17Last Post: 11-16-2009, 04:57 PM -
Math.random()
By Dieter in forum New To JavaReplies: 4Last Post: 09-14-2009, 09:28 AM -
[SOLVED] Help with math.random
By tomiu in forum New To JavaReplies: 10Last Post: 12-21-2008, 09:55 PM -
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11-23-2007, 02:09 PM -
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 03:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks