0

I am trying to assign a number 1-10 to two variables. If the number is greater than max then update max with that number, if the number is less than min, then update min with. All the while updating the total with whatever number gets generated. I thought I had it correct but whenever it generates. Then at the end I must average it. The number isn't random and get's stuck on the same few numbers. Not sure what I have wrong.

package java_programming;
import java.util.Random;

public class Jp{
    public static void main(String[] args){
        //The Power Operation
        double power = Math.pow(5.0,3.0);

        //The Square Root Operation
        double root = Math.sqrt(12.0);

        //The Max Operation
        double Maxi = Math.max(23.4, 23.5);

        //The Min Operation
        double Mini = Math.min(23.4, 23.5);

        //The Random Opeartion
        double rand = Math.random();

        System.out.println("Results");
        System.out.println("Math.pow = " + power);
        System.out.println("Math.sqrt = " + root);
        System.out.println("Math.max = " + Maxi);
        System.out.println("Math.min = " + Mini);

        System.out.println("Math.random = " + rand);
        Jp rs = new Jp();
        System.out.println("Min value");

        rs.randomStudy();
    }

    public void randomStudy(){
        int total = 0;
        int max = -1;
        int min = 11;
        int iterations = 1000;
        Random ran = new Random();
        for(int i = 0; i < iterations; i++){
            int count = ran.nextInt(10);
            min = Math.min(count, min);
            max = Math.max(count, max);
            total += count;
        }
        System.out.println("Result of max: " + max);
        System.out.println("Result of min: " + min);
        System.out.println("Average: " + (1.0 * total / iterations));
    }
}
vandale
  • 3,600
  • 3
  • 22
  • 39
SWIIPER
  • 3
  • 2
  • There is a reason if they are called pseudo-random generators! As stated above, you should seed your generator. – eddie Nov 20 '17 at 20:02
  • Seeding the generator will guarantee the same results, that is not the intended behavior. Using [new Random()](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#Random--) already seeds the generator randomly _"Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor."_ – phflack Nov 20 '17 at 20:05
  • I've added the new random() but my output still gets the same. But this time max: 0 min: 10 change Random rand = new Random(); for(int i = 0;i<1000;i++){ num=rand.nextInt(11); total =(num + total); if(num > max){ max = num; – SWIIPER Nov 20 '17 at 20:13
  • You might need to update your code in your question. – falcon Nov 20 '17 at 20:15
  • The changes I made are edited above. – SWIIPER Nov 20 '17 at 20:17
  • Why is it surprising that the same random distribution converges to the same summary statistics? – Louis Wasserman Nov 20 '17 at 20:18
  • @phflack is correct Random() auto-seeds. I would suggest turning your while loop down to <5 iterations until you work out your issue. I will have to re-compile. Initially the 'if' statements weren't in the for-loop – Daniel Gale Nov 20 '17 at 20:19
  • @LouisWasserman It's probably more surprising when every time it's used in the while loop it gives 11, when it should be giving 0-10 (look at the for loop) – phflack Nov 20 '17 at 20:24

1 Answers1

2

Looking only at randomStudy():

public void randomStudy()
{
    int total = 0;
    int max = -1;
    int min = 11;
    int i = 0;
    Random ran = new Random();
    while (i < 1001)
    {
        for(int count = 1; count<=10;count++)
            count = ran.nextInt(11);

        if(total < min)
            min = total;
        if(total>max)
            max = total;

        System.out.println("Result of max: " + max);
        System.out.println("Result of min: " + min);
        double average = total/1000;
        System.out.println("Average: " + average);
        i++;
    }
}
  • Initializes variables: total, max, min, i, ran
  • Loops 1000 times
    • Loops 10 times
      • sets count to [0, 10]
    • Checks and sets min/max
    • Prints min/max/average

  • Using a while loop can be done with a counter, but it's usually done with a for loop
  • Looping until you get a random number >= 10 doesn't make much sense, since you just want a random number
  • min/max are probably expected to be min/max of the random count, not of the total (and it's clearer to use Math.min/max)
  • Calculating the average should be done with double division, not with integer division
  • Normally when printing results, one expects overall results, not at each stage of calculation

Updated method:

public void randomStudy()
{
    int total = 0;
    int max = -1;
    int min = 11;
    int iterations = 1000;
    Random ran = new Random();
    for(int i = 0; i < iterations; i++)
    {
        int count = ran.nextInt(11);
        min = Math.min(count, min);
        max = Math.max(count, max);
        total += count;
    }

    System.out.println("Result of max: " + max);
    System.out.println("Result of min: " + min);
    System.out.println("Average: " + (1.0 * total / iterations));
}

With the questions edits:

[...]
while(i < 1001)
{
    for(int j = 0; j < 1000; j++)
    [...]

That will loop 1000*1000 (1000000) times, which may be a bit more than what you're trying to do


To get random numbers [1, 10] instead of [0, 10], use ran.nextInt(10) + 1 instead (How do I generate random integers within a specific range in Java?)

phflack
  • 2,729
  • 1
  • 9
  • 21
  • I would suggest making a variable out of the total loops so that you don't have to worry about it getting out of sync. (where you have 1000 and 1000.0) – Daniel Gale Nov 20 '17 at 20:26
  • 1
    Was about to add that, and then the OP changed the question a bit, trying to figure out how much changed – phflack Nov 20 '17 at 20:29
  • Hello, trying your method the value of Max stays the same at 10, and min stays at 0. I'm trying to reach overall results. I need to create a loop that runs 1000 times, generate a random int between 1-10. Add to the total, Then if it's less than min update min with the new number, if it's greater than max update max with the new number. – SWIIPER Nov 20 '17 at 20:32
  • @SWIIPER: why is it surprising that in a loop that generates 1000 random numbers between 0 and 10, there's a 0 and a 10 that come up at some point? Those are going to be the min and max of 1000 random numbers between 0 and 10, with ludicrously high probability. – Louis Wasserman Nov 20 '17 at 20:32
  • Also for some reason the person who edited my original comment changed a few things that weren't the original code that I had posted. So I had to re-do some of the things. – SWIIPER Nov 20 '17 at 20:32
  • @LouisWasserman I've ran the code multiple times and that seems to be the only thing that is chosen. That which leads me to believe it isn't random. – SWIIPER Nov 20 '17 at 20:34
  • @SWIIPER try lowering the number of iterations to 3. In lower numbers it will be very unlikely to get both a 0 and a 10 – phflack Nov 20 '17 at 20:37
  • @SWIIPER the chance of not getting a 0 out of 1000 chances of 0-10 is (10/11)^1000=4.05e-42=~0 – phflack Nov 20 '17 at 20:40
  • @phflack then I'm confused on why this is happening. I've updated the code with your method and I get "Result of max: 9" "Result of min: 0". Where it never changes from those two digits. No matter how many times I replay the code. – SWIIPER Nov 20 '17 at 20:43
  • @SWIIPER I'm using `int iterations = 3;` and `int count = ran.nextInt(10) + 1;` for testing if there is randomness in random (and produced min=6, max=8 one run, min=1, max=10 on another) – phflack Nov 20 '17 at 20:45
  • I've change my code above. That code generates the same min-max. It doesn't seem to actually generate different numbers, which is my goal. – SWIIPER Nov 20 '17 at 20:46
  • Hey. Just tried it. You've got it spot on. Thank you very much. – SWIIPER Nov 20 '17 at 20:47
  • 1
    @SWIIPER have you tried printing out the individual random numbers? But like phflack said above -- the probability of not getting min=0 and max=10 with 1000 elements is roughly equivalent to the chance of getting hit by lightning 14 times. – Louis Wasserman Nov 20 '17 at 20:48