Home static Java Programming [Archive] - arrays and Math.random
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - arrays and Math.random
 This topic has 10 replies on 1 page.

Posts:341
Registered: 03-06-12
arrays and Math.random
Aug 2, 2004 11:38 AM

i am playing around with arrays and i found this exercise to help me. A magic square of order n is an arrangement of integers 1 through n^2 in a square format such that the sum of numbers along any row, any column and any diagonal is the same.

so i have this code to generate a square n < 50

this code seems to work...but i think only with odd integers????
is there a way for me to only ask for a random odd interger using Math.random????

``public class square{public static void main(String args[]){  int i, j, k;  int n, magic_square[][];   magic_square = new int[50][50];    for ( i = 0; i < 50; i++)      for ( j = 0; j < 50; j++)           magic_square[i][j] = 0;       System.out.println("Magic Square will generate a square n < 50");      n = (int)Math.floor(Math.random()*50+1);      System.out.println("Random number choosen was "+n);      System.out.println("Below is the result of Magic Square of order "+n);            i = 0; j = n / 2;       for(k = 1; k <= n*n; k++)      {         magic_square[i][j] = k;         i--; j++;         if(i < 0 && j > n-1) {i += 2; j--;}         if(i < 0) i = n-1; if(j > n-1) j = 0;         if (magic_square[i][j] != 0) {i += 2; j--;}      }//end for       for (i = 0; i < n; i++)      {         for(j = 0; j < n; j++) System.out.print(magic_square[i][j]+"\t");         System.out.print("\n");      }//end for   }//end main}//end class square``

Posts:2,206
Registered: 8/15/02
Re: arrays and Math.random
Aug 2, 2004 11:40 AM (reply 1 of 10)

Ask for a random integer between 0 and 25, and then multiply by two

Posts:341
Registered: 03-06-12
Re: arrays and Math.random
Aug 2, 2004 11:44 AM (reply 2 of 10)

n = (int)Math.floor(Math.random()*(25+0)*2);

when i use this line of code
i still get an even number at times

Posts:2,206
Registered: 8/15/02
Re: arrays and Math.random
Aug 2, 2004 12:02 PM (reply 3 of 10)

My bad, you want odd numbers between 0 and 50?
``Random r = new Random();int n = r.nextInt(26);n = 2*n-1;``

You may want to make the Random object a class variable, not local to the method.

Posts:87
Registered: 00-05-05
Re: arrays and Math.random
Aug 2, 2004 12:09 PM (reply 4 of 10)

That returns -1 if n==0.
``int n = r.nextInt(25);n = 2*n + 1;``

Posts:341
Registered: 03-06-12
Re: arrays and Math.random
Aug 2, 2004 12:09 PM (reply 5 of 10)

do i need to import a library to use Random

Posts:2,206
Registered: 8/15/02
Re: arrays and Math.random
Aug 2, 2004 12:10 PM (reply 6 of 10)

Actually, that won't quite work either since you could get -1. I guess better would be
``Random r = new Random();int n = ( 2.0 * r.nextInt(25) ) +1;``

That should do nicely

Posts:2,206
Registered: 8/15/02
Re: arrays and Math.random
Aug 2, 2004 12:11 PM (reply 7 of 10)

do i need to import a library to use Random

Yep, either check the API or trust me
``import java.util.Random;``

Assuming you are using some variant of 1.4. The Random class is not available in 1.3

Posts:2,206
Registered: 8/15/02
Re: arrays and Math.random
Aug 2, 2004 12:12 PM (reply 8 of 10)

It must be Monday...
``int n = ( 2 * r.nextInt(25) ) +1;``

Posts:341
Registered: 03-06-12
Re: arrays and Math.random
Aug 2, 2004 12:20 PM (reply 9 of 10)

thanks...you saved me i was actually trying to make this more complicated than ever
``     n = (int)Math.floor(Math.random()*50+1);      Random r = new Random();      n = ( 2 * r.nextInt(25) ) +1;``

i was going for a while loop and a huge headache...

``while (n != 0){if(n % 2 == 0){//n was even..ffnd another n//math.random}//end ifelse{//n was odd print square//insert code}/end else}//end while``

Posts:11,186
Registered: 06.04.04
Re: arrays and Math.random
Aug 2, 2004 1:24 PM (reply 10 of 10)

Even if all that odd/even random number generation stuff were solved, you'd still need different algorithms
generating those odd/even sized magic squares. That's the core of all the fun of this exquisite little problem.

kind regards,

Jos

 This topic has 10 replies on 1 page.