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 7 of 7

Thread: how to use nextInt() to generate random integers?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default how to use nextInt() to generate random integers?

    So basically for my assignment, I need to generate a random integer number between 20 and 90. My textbook leaves a lot to be desired in the explanation of using nextInt(). I want to assign the random integer value to an int that I have already declared called randomAngle. Can anyone explain to me the correct syntax to get this?
    I hope I've made myself clear, many thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to use nextInt() to generate random integers?

    using nextInt()
    What class is the nextInt() method in?
    The API doc for the class and that method should explain what it does.
    Link to the API doc:Java Platform SE 6

  3. The Following User Says Thank You to Norm For This Useful Post:

    rph (July 22nd, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to use nextInt() to generate random integers?

    Figured it out now, all i had to do was write:

    static Random randGen = new Random();
    int randomAng = randGen.nextInt(70) + 20;

    in the public class and make sure I put:

    import java.util.Random;

    at the beginning.
    Many thanks for your help

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: how to use nextInt() to generate random integers?

    static Random randGen = new Random();
    int randomAng = randGen.nextInt(70) + 20;
    Your code will generate random numbers from 20 to 89 inclusive. Read the API to find out why 90 will be excluded.
    Improving the world one idiot at a time!

  6. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to use nextInt() to generate random integers?

    why we need static?

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: how to use nextInt() to generate random integers?

    This is an old thread. And I'm sure you don't want to give the impression that you are using this forum to plant links to your site.

    The link adds nothing beyond that which Junky has already alluded to. In particular it does not address the question of why the OP chose to make the variable static. It's a perfectly reasonable thing to do, but without knowing what his or her intentions were we can only guess (pointlessly) at the reason.

Similar Threads

  1. generate a random number from 100 to 150, inclusive in applet
    By chonch in forum Java Theory & Questions
    Replies: 12
    Last Post: February 14th, 2014, 05:44 AM
  2. 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
  3. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  4. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM

Tags for this Thread