Results 1 to 9 of 9

Thread: Random Integers

  1. #1
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default Random Integers

    First off, I am a noob when it comes to programming java. I am a high school student, and have only taken a semester course on java. I am currently working on making a game with simple integer/picture tricks, but I need to know how to create a code that can randomize numbers in between set parameters. IE: A number in between 0 - 20.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Random;
    
    public class Test {
        static Random seed = new Random();
    
        public static void main(String[] args) {
            test(0, 20);
            test(5, 10);
        }
    
        private static void test(int low, int high) {
            int min = Integer.MAX_VALUE;
            int max = -Integer.MAX_VALUE;
            int range = high - low +1;
            for(int j = 0; j < 500; j++) {
                int n = low + seed.nextInt(range);
                if(n < min) min = n;
                if(n > max) max = n;
            }
            System.out.printf("min = %d  max = %d%n", min, max);
        }
    }

  3. #3
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    could you add some text explaining each part of that code please...I don't really understand about half of that (sorry, I am kind of garbage at java)

  4. #4
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    or if anyone could help out....

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Its an idea of how to explore a question.
    Java Code:
    private static void test(int low, int high) {
        // Find the lowest and highest random values.
        // Save the extreme values in variables to print later.
        // Initialize the minimum variable to the highest
        // possible value for its type (data type [i]int[/i])
        int min = Integer.MAX_VALUE;
        // Initialize the maximum value to the lowest
        // possible value for its data type.
        int max = -Integer.MAX_VALUE;
        // Compute random values in the range [low <= value <= high].
        // This is the part you play with to get the results you want.
        int range = high - low +1;
        // Do enough runs to get definite accurate min and max:
        for(int j = 0; j < 500; j++) {
            int n = low + seed.nextInt(range);
            // If the value "n" is lower than the
            // current/last-saved value of "min"
            // save the value of "n" in "min".
            if(n < min) min = n;
            // Update "max" with the highest value we find.
            if(n > max) max = n;
        }
        // Print out the results.
        System.out.printf("min = %d  max = %d%n", min, max);
    }
    All of this was a journey to be able to write this:
    Java Code:
    int n = low + seed.nextInt(high - low +1);
    with confidence.

  6. #6
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    that is still kind of confusing, idk if you want to waste any more time trying to explain it to me, but if you could that would be way nice (like which stuff I will need to modify to my own applet and stuff)

  7. #7
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    for example the integers and values, which ones do I change and which ones are supposed to be left untouched

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The best way to figure out is to play with the code given in #2 above. When someone posts code that compiles and runs you can run it, make changes, experiment and learn how it works. Learning comes with tinkering and experimenting.

  9. #9
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 05:45 PM
  2. Finding Median of X Integers
    By Hasan in forum New To Java
    Replies: 3
    Last Post: 08-12-2008, 02:06 PM
  3. Replies: 4
    Last Post: 04-16-2008, 01:26 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. Convert roman numerals to integers
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 11:27 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
  •