DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    Generating a random binary array

    I'm new to java so it is possible i haven't spotted something simple here but its been bugging me for hours so here goes.

    I've written a class that creates an array of ones and zeros at random and then prints this in the command line (or should do).

    My code:
    class Genpop {
    public static int[][] genpop1() {
    int[][] population = new int[8][8];
    for(int i=0;i<8;i++){
    for(int j=0;j<8;j++){
    double x = java.lang.Math.random();
    if (x<0.5){
    population[i][j]=0;}
    else{
    population[i][j] =1;}
    }
    }
    return population;
    }
    public static void main(String args[]) {
    int[][] pop = genpop1();
    System.out.println(pop);
    }
    }

    I've tried print lines to check that the random number is being generated and to make sure that the if statements are being executed but all i get when i run the program is:

    [[I@f4a24a

    not very nice

    any help would be greatly appriciated
    cheers

  2. #2
    Join Date
    Dec 2005
    Posts
    4

    getting there...

    i have nearly solved my own problem

    replacing: System.out.println(pop)
    with:
    for(int i=0;i<8;i++){
    for(int j=0;j<8;j++){
    System.out.println(pop[i][j] + " ");
    }
    System.out.println();
    }

    However this outputs the zeros and ones in one long vertical line, i would prefer them in a square as if i were just just printing the array instead of eac individual element.

    Any tips greatfully accepted

  3. #3
    Join Date
    Dec 2005
    Posts
    4

    solved

    replace System.out.println(pop[i][j] + " ")
    with System.out.print(pop[i][j] + " ")

    sorry about abuse of message board
    just a beginner

Similar Threads

  1. Generating Random Number/ID
    By ajames420 in forum VB Classic
    Replies: 4
    Last Post: 11-28-2005, 03:46 PM
  2. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 06:16 AM
  3. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM
  4. Random Array
    By XRsTX in forum VB Classic
    Replies: 7
    Last Post: 02-28-2005, 02:02 PM
  5. generating unique random numbers
    By Dave in forum Java
    Replies: 3
    Last Post: 05-14-2002, 08:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links