Results 1 to 6 of 6
  1. #1
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default Random Numbersz?

    public class mainMemory {

    public static void main(String[] args) {
    int frag1=100, frag2=200, frag3=300, frag4 ;
    frag4=frag1+frag2+frag3;
    // TODO, add your application code
    System.out.println("Internal Frag: " +frag4 +" Kb");
    }


    }


    Hi, I need a little help here, as u can see from the code above, frag4 is the combination of frag1+frag2+frag3. How do I set a random number to frag1, frag2, frag3? in addition the random number must not be greater than 999

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    dun really understand what they meant. any good webbies that i could have hands on?
    I'll repost this in right forums
    Last edited by PeterFeng; 01-13-2009 at 07:42 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

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

    Default

    See the java.util.Random class. I also have a few web pages discussing random numbers in Java, and some pitfalls to be aware of.

    The main thing that people get wrong when they use the Random class (and actually various other classes) is simply that they make things up rather than reading the documentation. Typical things that people get wrong:
    - they create a new Random object for every single random number they need; don't do that: instead, create a single Random object and re-use it
    - they don't use the appropriate method; for example, if you need a number between 0 and 1000, then call nextInt(1000): do not do the following

    Java Code:
    // This is WRONG!!
    int n = Math.abs(r.nextInt()) % 1000;

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Random
    By koolhoney in forum New To Java
    Replies: 11
    Last Post: 09-30-2008, 03:39 AM
  3. Using Random
    By razmyasdfg in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-27-2008, 10:47 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. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 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
  •