Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: generate a random number from 100 to 150, inclusive in applet

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default generate a random number from 100 to 150, inclusive in applet

    im not sure how to code the generated a random number from 100 to 150 inclusive for an applet


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: generate a random number from 100 to 150, inclusive in applet

    What have you tried? What did google tell you?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: generate a random number from 100 to 150, inclusive in applet

    in goggle i found

    int Low = 10;
    int High = 100;
    int R = r.nextInt(High-Low) + Low;

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: generate a random number from 100 to 150, inclusive in applet

    but r.nextInt(High-Low) + Low i know isnt applet

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: generate a random number from 100 to 150, inclusive in applet

    ...what?

    Please see the link in my signature on asking smart questions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: generate a random number from 100 to 150, inclusive in applet

    Quote Originally Posted by chonch View Post
    but r.nextInt(High-Low) + Low i know isnt applet
    Lesson: Applets (The Java™ Tutorials > Deployment)

    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    import javax.swing.JLabel;
     
    public class HelloWorld extends JApplet {
        //Called when this applet is loaded into the browser.
        public void init() {
            //Execute a job on the event-dispatching thread; creating this applet's GUI.
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        JLabel lbl = new JLabel("Hello World");
                        add(lbl);
                    }
                });
            } catch (Exception e) {
                System.err.println("createGUI didn't complete successfully");
            }
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: generate a random number from 100 to 150, inclusive in applet

    if java.util.Random is available in applet environment then i don't see a problem. The thing is r.nextInt() where r is an instance of Random, produces negative value. So to produce a number between 100 and 150 inclusive u do is this:
    int a = r.nextInt() % 151 ;
    a = a > 0 ? a : -1 * a ; // making it positive
    if (a > 150){
        a %= 150 ; // cutting down to size
    if (a < 100)
        a += 100 ;
    if i misunderstood ur problem then sorry, very much sorry.
    Last edited by dumb_terminal; April 12th, 2011 at 03:06 PM.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: generate a random number from 100 to 150, inclusive in applet

    Quote Originally Posted by dumb_terminal View Post
    if java.util.Random is available in applet environment then i don't see a problem. The thing is r.nextInt() where r is an instance of Random, produces negative value. So to produce a number between 100 and 150 inclusive u do is this:
    int a = r.nextInt() % 151 ;
    a = a > 0 ? a : -1 * a ; // making it positive
    if (a > 150){
        a %= 150 ; // cutting down to size
    if (a < 100)
        a += 100 ;
    if i misunderstood ur problem then sorry, very much sorry.
    ...are you serious?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: generate a random number from 100 to 150, inclusive in applet

    Quote Originally Posted by KevinWorkman View Post
    ...are you serious?
    Exactly what I thought
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. #10
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: generate a random number from 100 to 150, inclusive in applet

    Quote Originally Posted by dumb_terminal View Post
    int a = r.nextInt() % 151 ;
    a = a > 0 ? a : -1 * a ; // making it positive
    if (a > 150){
        a %= 150 ; // cutting down to size
    if (a < 100)
        a += 100 ;
    Ugh, seems to me like this is all he needs:

    int a = r.nextInt() % 51 + 100;

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: generate a random number from 100 to 150, inclusive in applet

    Quote Originally Posted by Gerp View Post
    Ugh, seems to me like this is all he needs:
    Why use the modulus operator at all? You're a lot closer to being correct than dumb_terminal, but I don't see a need for modulus here at all.

    Besides, spoonfeeding is NOT helpful to the OP. He's probably long gone by now anyway.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Re: generate a random number from 100 to 150, inclusive in applet

    i used this code on my hide and seek college project, where a certain circle will randomly display on the specified playing area.

    my playing area is g.fillRoundRect(245,185,280,280,15,15);

    it was adjusted so that it will not touch its border.

    ranX = 1 + (int)((530-260)*Math.random()+245);
    ranY = 1 + (int)((460-190)*Math.random()+175);

  13. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: generate a random number from 100 to 150, inclusive in applet

    Please check the date of the thread and the date of the last post. If the last post is older than ~3 weeks, it's dead. Let it rest in peace.

    Thread closed.

  14. The Following User Says Thank You to GregBrannon For This Useful Post:

    KevinWorkman (February 18th, 2014)

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  3. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 12:00 PM
  4. HELP. Random Number Between
    By Raymond Pittman in forum Java Theory & Questions
    Replies: 3
    Last Post: February 15th, 2011, 09:50 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM