Results 1 to 10 of 10

Thread: Math.random

  1. #1
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default 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 12:18 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

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

    Default

    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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    *ahem*
    Random (Java Platform SE 6)

    psssst: click the link!

  6. #6
    852000 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default hope help you

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

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    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. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default

    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. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by quad64bit View Post
    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. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Quote Originally Posted by masijade View Post
    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

Similar Threads

  1. use Math.random and then count occurrences
    By iliak67 in forum New To Java
    Replies: 17
    Last Post: 11-16-2009, 04:57 PM
  2. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 09:28 AM
  3. [SOLVED] Help with math.random
    By tomiu in forum New To Java
    Replies: 10
    Last Post: 12-21-2008, 09:55 PM
  4. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 03:31 AM

Posting Permissions

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