Results 1 to 8 of 8
  1. #1
    Ciwan's Avatar
    Ciwan is offline Member
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    88
    Rep Power
    0

    Exclamation Random Numbers from 1 onwards

    Hi Guys

    can somone please tell me what I need to do to the nextInt (int n) to make it generate numbers starting at 1 and not 0.

    Thanks
    >> What can be asserted without proof can be dismissed without proof. <<

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

    Default

    I've always found that adding 1 to the result works well for me.

    i.e., if I want a random int from 1 to 10:
    Java Code:
    int max = 10;
    Random random = new Random();
    int myRandomInt = random.nextInt(max) + 1; //violá

  3. #3
    Ciwan's Avatar
    Ciwan is offline Member
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    88
    Rep Power
    0

    Default

    can you show me an example of what you mean please :(
    >> What can be asserted without proof can be dismissed without proof. <<

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default hhhmmm...

    A couple of ideas:
    Java Code:
    if (n==0)
     {
       n=1;
     }
    or
    Java Code:
    int randInt = rand.nextInt(n) [B]+ 1[/B];
    not sure on the syntax of the above, but you get the idea

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    look above

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default What?

    How in hell did you do that Furball? :) That was sneaky... Oh, well... the OP now has lots of options to pick from....
    And speaking of the OP, Ciwan, what do you mean you need an example? Fubarable's post is a complete and functional example (more code than I posted).

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    By the way, always add 1. If you do the other suggestion (testing for 0 and replacing with 1), then the result will be biased towards producing 1 (since there are now two values that can end up with this number).

  8. #8
    Ciwan's Avatar
    Ciwan is offline Member
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    88
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by CJSLMAN View Post
    Ciwan, what do you mean you need an example? Fubarable's post is a complete and functional example (more code than I posted)
    Hi Guys

    Thank you very much Fubarable :) that worked perfectly :D

    @CJSLMAN: when I asked Fubarable for an example, he did not have an example up ! that came after I asked :)

    Anywhoo Thank you for all your help :)
    >> What can be asserted without proof can be dismissed without proof. <<

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 02:20 PM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 02:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 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
  •