User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 375,206 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,344 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 471 | Replies: 5
Reply
Join Date: May 2008
Posts: 2
Reputation: geisteskrankhei is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
geisteskrankhei geisteskrankhei is offline Offline
Newbie Poster

random numbers into an array

  #1  
May 13th, 2008
Basically, I have to make a code that requests user input. The program asks them to tell me how many columns/rows the user wants the magic square to have. I need to then generate random numbers for each spot.

ex: user inputs 3, out put looks like

3 7 1
6 4 9
8 2 5

I know how to generate a single random number but I've been trying to figure out how I'm going to get as many as the user needs.

Numbers cannot be duplicated, but I can tackle that if statement on my own easily. it's just the part that involves multiple random numbers put into an array that I am having difficulty on. Any help would be greatly appreciated. So far, my code looks like:

 import java.awt.*;
import hsa.Console;
import java.util.Random;

public class MagicSquare
{
    static Console c;           

    public static void main (String[] args)
    {
        c = new Console ();

        Random random = new Random ();
        c.println ("How many numbers up/down and left/right would you like in your magic square?");
        int size = c.readInt ();
        int NumberOfRandoms = (size * size);
        int myRandomNumber = random.nextInt (NumberOfRandoms) + 1;
        c.println (myRandomNumber);

        
    } 
} 
Last edited by geisteskrankhei : May 13th, 2008 at 6:45 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 385
Reputation: javaAddict is on a distinguished road 
Rep Power: 1
Solved Threads: 35
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Whiz

Re: random numbers into an array

  #2  
May 14th, 2008
Since the user inputs the size you can create the array:

int [][] array=new int[N][N];

Then use a for-loop to put numbers in the array.

As I noticed at your example the random numbers are from 1 to 9. Is that a requirement? Meaning do the numbers have to be from 1 to N*N?
I AM the 12th CYLON
Reply With Quote  
Join Date: May 2008
Posts: 2
Reputation: geisteskrankhei is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
geisteskrankhei geisteskrankhei is offline Offline
Newbie Poster

Re: random numbers into an array

  #3  
May 14th, 2008
Yes, it must be from 1 to N*N, with no numbers repeating. To make it easier I was thinking about making a for loop to generate numbers from 1 to N*N, not randomly. But, put them into the array and then shuffle them. I am having trouble generating randoms and putting them into a for loop. I have:

        int [] [] Randoms = new int [size] [size];
        for (int row = 0 ; row < Randoms.length ; row++)
            for (int col = 0 ; col < Randoms [0].length ; col++)
                Randoms [row] [col] = myRandomNumber;
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,504
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 10
Solved Threads: 252
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: random numbers into an array

  #4  
May 14th, 2008
You're on the right track generating the list of numbers 1 to N*N into an array and then shuffling them. You just need to consider how to pick a random place in the array to place each number.
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 385
Reputation: javaAddict is on a distinguished road 
Rep Power: 1
Solved Threads: 35
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Whiz

Re: random numbers into an array

  #5  
May 14th, 2008
One suggestion is this:
you could have an array with values from 1 to N*N like this: (example)
int [] oneDim= {1,2,3,4,5,6,7,8,9};

Generate a random number from 0 to 8.
Take that number and put it inside the 2-sized array.Use a for loop that puts the first taken number to (0,0).
Then use a swap method that takes the selected number and puts it at the end of the 1-sized array:
example: if the random number was 4, Then the oneDim[4]=5 will be put at the target 2-sized array, and after the swap the oneDim will be like this: {1,2,3,4,9,6,7,8,5}

Then you will repeat the above procedure with one difference: the random generated number will be from 0 to 7. So when you go to the oneDim array you will take only the first 8 numbers: {1,2,3,4,9,6,7,8} and you will not take the last which is already used: 5

int [] oneDim= new int[N]
int [][] randoms=[N][N];

for (int i=0;i<N;i++) {
  oneDim[i]=i+1;
}

int row=0;
int col=0;

for (int i=0;i<N;i++) {
  int r = //random number from 0 to N-1

  //put the value from oneDim to randoms
  randoms[row][col] = oneDim[r];

  //swap oneDim[r]  with oneDim[N-1 - i]  //N-1 - i : so not to keep swapping a number that was previously used and was put at the end 

  //alter row, col so as to put the next number at a new place
}
I AM the 12th CYLON
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,504
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 10
Solved Threads: 252
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: random numbers into an array

  #6  
May 14th, 2008
You really don't even need to have a 2D array. The single 1D array is fine for a uniform rectangular grid, with rows being nothing more than an offset into the array.

I hesitate to say any more though since it's a homework situation.
Last edited by Ezzaral : May 14th, 2008 at 3:12 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 2:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC