-4

I'm working on a Bubblesort that's supposed to take a random array of numbers and return them but I keep getting a 'possible loss of precision' error when compiling that tells me Math.random is returning a double and I need an integer. How can I covert this?

 public class Bubblesort {

  public static void main(String[] args) {

    //create an int array we want to sort using bubble sort algorithm
    int intArray[] = new int [Math.random()*100];

Here's my code just in case.

Edit: There was more code involved that I didn't think I would need to include. I tried casting but System.out.println isn't working now.

Rest of the code:

 public static void main(String[] args) {

    //create an int array we want to sort using bubble sort algorithm
    int intArray[] = new int [(int)Math.random()*100];

    //int intArray[] = new int [100];

    //print array before sorting using bubble sort algorithm
    System.out.println("Array Before Bubble Sort");
    for(int i=0; i < intArray.length; i++){
      System.out.print(intArray[i] + " ");
    }

    //sort an array using bubble sort algorithm
    bubbleSort(intArray);

    System.out.println("");

    //print array after sorting using bubble sort algorithm
    System.out.println("Array After Bubble Sort");
    for(int i=0; i < intArray.length; i++){
      System.out.print(intArray[i] + " ");
    }
yizzlez
  • 8,757
  • 4
  • 29
  • 44
Reddy
  • 1
  • 2

8 Answers8

6

Don't forget to enclose it in the parenthesis before downcast to int otherwise it will converted to zero always because Math.random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

int intArray[] = new int [(int)(Math.random()*100)];
Braj
  • 46,415
  • 5
  • 60
  • 76
6

An alternative to type casting, you could just use Random instead:

Random r = new Random();
int retval = r.nextInt(100);
nickb
  • 59,313
  • 13
  • 108
  • 143
3

Use java.util.Random, with nextInt(int n)

Random random = new Random()
int intArray[] = new int [random.nextInt(100)];

From the Random javadoc for nextInt(int n):

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

lreeder
  • 12,047
  • 2
  • 56
  • 65
2

You can type cast

int intArray[] = new int [(int)(Math.random()*100)];
Vishrant
  • 15,456
  • 11
  • 71
  • 120
0

There is a method called casting for that.

new int [(int) Math.random()*100];

It converts the return value into the data type used in the casting method.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
0

try this,

int randomWithRange(int min, int max){

       int range = (max - min) + 1;     
       return (int)(Math.random() * range) + min;
}
Mani
  • 3,394
  • 3
  • 30
  • 36
0

Just for clarification: You are creating an array with random length and not random values. Is this intended?

if not:

int intArray[] = new int [somesize];
for(int i=0;i<intArrey.length();i++)
{
  intArrey[i]= (int)(Math.random()*100);
}
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
  • I think it very much provides a solution to his question. The array elements are never initialized so I think he mixed up creating an array and initializing its values. – Dawnkeeper May 23 '14 at 14:26
  • @Dawnkeeper If you would use a different way of order, I think there would be not problem. – Reporter May 23 '14 at 14:33
-1

The right way to create a random integer i is:

Double d = Math.random()*100;
int i = d.intValue();