5

I'm not too familiar with Math.random(); and I don't know what to do for the conditions that I want. I want to produce a random integer between 0 and 52 so far this is what I have set up.

    public class Tester{
    public static void main(String args[]){
        int size=52;
        while(size>0){
            int rando=(int)Math.random()*size;
            size--;
            System.out.println(rando);
        }
    }
}

My code prints out all 0's until the condition of the while loop is met. I wanted to know how I would produce a random integer between 0 and 52. I understand that Math.random(); produces a double and I think theres a problem with the type casting. Thanks.

Nodcah
  • 3
  • 3
Tariq Al-Attrash
  • 99
  • 1
  • 1
  • 7

6 Answers6

7

You only cast Math.random(). Its a value between 0 and 1 (excluding 1). If you cast this it's zero anyway.

Cast the whole expression: (int)(Math.random()*size);

BTW: Your interval is only from 0 to 51 (because of the excluding 1.

Use (int)(Math.random()*(size+1));, if you want 0...52 as your interval.

osanger
  • 2,276
  • 3
  • 28
  • 35
3

The cast takes precedence over the multiplication. Since Math.random() returns a double in the range [0.0..1.0), it will always be converted to 0, and the result of multiplying that by any other int will of course be 0. You could perform the multiplication before casting - ((int)(Math.random()*size)), but really it'd be easier to use Random.nextInt(int).

Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

Math.random() returns a pseudo-random number with a domain of [0 , 1).

In the line:

int rando=(int)Math.random()*size;

this value is cast as an int and then multiplied by size. To fix the problem with only printing zeros you need to add parentheses.

int rando = (int) (Math.random()*size);

This will only give you numbers in the domain [0, 51) To fix this:

int rando = (int) (Math.random()* (size + 1);
star2wars3
  • 83
  • 1
  • 10
1

Necessary to use the Math class?

a simpler way to find this number would using the Random class

Example:

Random randomNumber = new Random ();
System.out.println(randomNumber.nextInt(53));

See also:

Math.random() versus Random.nextInt(int)

Community
  • 1
  • 1
Douglas Ribeiro
  • 2,867
  • 1
  • 18
  • 12
0

You can use another code like java.util.Random:

Random r = new Random();
int rando = r.nextInt(53); //inclusive 0 and exclusive 53

In case of Math.random() and your code, you have to cast after the multiplication (set the brackets like below):

int rando = (int)(Math.random() * size);

In your code, the return value of Math.random() gets casted first and then it is multiplied with the size-parameter. Because Math.random() return double between 0 and 1, the cast to int returns 0.

Supahupe
  • 515
  • 2
  • 11
0

You cannot use Math.random() to generate a random integer within a range of numbers, as seen in the docs.
But you may use the Random class comming in the java.util package, and use the method .nextInt() or more specifically .nextInt(int maximum) where maximum will be 53, not 52 because as docs state, the method is exclusive.
If you want a number between a specific range like 10 and 50, you may construct a method like this:

 private final Random RANDOMISER = new Random();

 private int generateRandomNumber(int from, int to){
  return RANDOMISER.nextInt((to+1)-from)+from;
 }
fill͡pant͡
  • 1,147
  • 2
  • 12
  • 24