Results 1 to 8 of 8

Thread: Random int

  1. #1
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default Random int

    Hi guys, been looking online and can;t seem to find how to do what I want. I'm basically looking through methods but want to create a random number. I know how to limit its top end but not sure how to limit both:

    For example a random number between 14-40.

    Any help is appreciated!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by drucey View Post
    Hi guys, been looking online and can;t seem to find how to do what I want. I'm basically looking through methods but want to create a random number. I know how to limit its top end but not sure how to limit both:

    For example a random number between 14-40.

    Any help is appreciated!
    Think of it: you want a pseudo random number in the range 14-40; in your mind subtract 14 from both numbers: 0-26. If you can generate a pseudo random number in that range you can generate one in the original range: simply add 14 to it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    just add the lower end to the number. If you want to do 14-40, you do the random from 0 - 26, then add 14.

  4. #4
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    Oh ok thanks. I did think of that just thought there may be a way to dictate the lower parameter!

    Thanx Guys

  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 drucey View Post
    Oh ok thanks. I did think of that just thought there may be a way to dictate the lower parameter!
    There is, and Jos and sunde887 show you how. ;)

  6. #6
    drucey is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks, got it.

    One more quick thing - the random generator is throwing out random numbers. What if i didnt want it to pick the same number? I have created the array etc, and want an if statement to say if its equal to ANYTHING in the array?

    Thanks guys

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by drucey View Post
    Thanks, got it.

    One more quick thing - the random generator is throwing out random numbers. What if i didnt want it to pick the same number? I have created the array etc, and want an if statement to say if its equal to ANYTHING in the array?
    There are two approaches to solve this little problem:

    1) remember the numbers that were already genetated; if such a number is generated again, retry.

    2) generate all possible numbers in some order and store them in a Collection (without repetition, the order doesn't mind) and call the Collections.shuffle( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    1) remember the numbers that were already genetated; if such a number is generated again, retry.
    Remember that if you use this method to use an efficient data structure for lookups, like a HashMap or something. If you use an ArrayList or the like with the .contains() method, this will get really slow really quick. Hashed structures have a constant time lookup, so they are quite fast for this type of thing!

Similar Threads

  1. Random
    By Arnold in forum New To Java
    Replies: 13
    Last Post: 11-14-2009, 10:53 AM
  2. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  3. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  4. Random
    By koolhoney in forum New To Java
    Replies: 11
    Last Post: 09-30-2008, 03:39 AM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

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