Error using "for" to generate 100 random numbers in java eclipse and compare them to the user value

Viewed 86

I am brand new to java and I have this problem that I always get 0 random numbers greater than the user value, and I should be getting a different amount of random numbers greater than the value every time I run the program

package chapterfinal;
import java.util.Scanner;

public class finalf {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner (System.in);
    int greaterOrequal = 0;
    int count = 0;
    final int finalcount = 100;
    System.out.println("Enter a value between 30 and 70: ");
    int value = input.nextInt();
    
    while (value > 70 || value < 30) {
        System.out.println("Please try to enter a value between 30 and 70: ");
        value = input.nextInt();
        
    }
    
    for (count = 0; count < finalcount; count++) {
        int random = (int) Math.random() * 100;
        if (random >= value)
                greaterOrequal++;
    }
        System.out.println("There are " + greaterOrequal + " random numbers greater than " + value);
            
} }

the "while" code is to prompt the user to reenter a value between 30-70 in case the user enters something out of that range.

the "for" code is to generate 100 random numbers and analyze how many are greater than the value that the user entered.

And finally just print the result of how many randoms were greater than the value and print the value that the user entered

I know is a really simple code, I can't find what is wrong with it!

This is the result that I get:

Enter a value between 30 and 70: 
80
Please try to enter a value between 30 and 70: 
50
There are 0 random numbers greater than 50

And it should be something like:

Enter a value between 30 and 70:
25
Please try to enter a value between 30 and 70:
50
There are 51 random numbers greater than 50

I should get a different amount of random numbers greater than the user value every time I run the program.

I hope any of you guys can help me

Please any help is appreciated!!

1 Answers

int random = (int) Math.random() * 100;

That doesn't parse the way you think it does.

That will:

[A] generate a number between 0 and 1.

[B] Cast that number to an int, so, now it is always 0.

[C] then multiply that by a 100. Still 0.

In other words, that parses as ((int) Math.random()) * 100.

The simplistic fix is (int)(Math.random() * 100), but note that this is NOT the right way to get a random number between 0 and 99.

The pigeon hole principle applies: Computers aren't magical; there are X unique numbers between 0 and 1 that your program can actually produce. X is high, but not infinite. (It's somewhere around 2^53).

If X cannot be divided fully by 100 (spoiler: It can't), then there will be numbers between 0 and 100 that are picked more often than others: It is not uniform.

You're somewhat unlikely to notice (the effect will be small), but that explains why the formula, fundamentally, is broken.

The proper way to do it, is as follows:

Random r = new Random(); // do this only once.

int x = r.nextInt(100); // x can be 0, 99, or anything in between.

It's also cleaner code, in that, without formulas, it's much harder to mess up your parentheses.

Related