Results 1 to 3 of 3
  1. #1
    hifriend is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Random number generator with range.

    Hi

    Really struggling with this!
    As part of an assignment for uni I have been given a code that produces a random number. I have been asked to amende the code so that it print a random integer in the range 1 to 1000.
    The code I have been given is:

    import java.util.Random;

    public class RandomSample
    {
    public static void main(String args[])
    {
    int n=6;
    int value;

    Random num_gen = new Random();
    value = num_gen.nextInt();
    value = Math.abs(value);
    value = value%n;
    value++;
    System.out.println(value);
    }
    }



    Does anybody have a clue what changes I need to make to put the range in?
    Thanks in advance! x

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: Random number generator with range.

    Why don't you take a look at the API?

    Java Platform SE 6
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    javaTurtle is offline Member
    Join Date
    Nov 2011
    Location
    Arizona
    Posts
    10
    Rep Power
    0

    Default Re: Random number generator with range.

    Here is a quick article on Random with a range specified. Try it out and see if you can get it to work.

    Java: generating random number in a range - Stack Overflow

Similar Threads

  1. Random Phone number generator
    By elecleoalune in forum New To Java
    Replies: 15
    Last Post: 05-19-2014, 12:37 PM
  2. Random Number Generator with User Input
    By person287 in forum New To Java
    Replies: 14
    Last Post: 07-10-2011, 10:49 AM
  3. Help with Random Number Generator
    By celtics in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 08:18 PM
  4. Random number generator
    By zerwik in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 12:10 PM
  5. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 06:47 PM

Tags for this Thread

Posting Permissions

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