Results 1 to 5 of 5
  1. #1
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Math.random() without a "0"

    Hi Java fans, I've been writing a tic-tac-toe game. Im thinking of a way that would allow me to produce random digits between 1 and 3 included. I know Math.random() starts with a 0 but i really don't need it. Anyone have their own sort off techniques that could be shared?

    Much appreciated

    Thanks
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Just add one to your result :)

    Java Code:
    Random r = new Random();
    
    int randValue = r.nextInt(3) + 1;
    My Hobby Project: LegacyClone

  3. #3
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Quote Originally Posted by mrmatt1111 View Post
    Just add one to your result :)

    Java Code:
    Random r = new Random();
    
    int randValue = r.nextInt(3) + 1;

    Hmmmmm...

    but what if the random value turns out to be 3? 3+1 = 4 = Outof BoundsException

    EDIT: AHHHHHHH...see i did not realise that you have used Random instead of Maths.random() My apologies. Thanks for the help.
    Last edited by FlyNn; 03-01-2010 at 09:54 PM.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Math.random() should work also:

    Java Code:
    int i = (int)(Math.random() * 3) + 1;
    My Hobby Project: LegacyClone

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by FlyNn View Post
    Hmmmmm...

    but what if the random value turns out to be 3? 3+1 = 4 = Outof BoundsException
    Hmmmm, boy this is tough,.... maybe you could limit your random numbers from 0-2 perhaps and then add 1? Nah, it can't be that simple, can it?

    And there's no need to use Math.random() and have to cast to int. r.nextInt(2) + 1 would work just fine.

    Just curious: did you try to figure this out yourself first before asking the forum?
    Last edited by Fubarable; 03-02-2010 at 01:42 AM.

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. Replies: 2
    Last Post: 10-08-2009, 11:28 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •