Yuita A. Sari

“If you want to be a writer, you must do two things : read a lot and write a lot. There's no way around these two things that I'm aware of, no shortcut.” ― Stephen King

Matrix : Random Binary ~ Java

Leave a comment

This is an example of matrix two dimension which is contain binary random. We can use class Random in Java

public class RandomBinarySet {
    public static void main(String[] args) {
        Random random = new Random();
        int[][] inputArray = new int[5][5];
        for (int i = 0; i < inputArray.length; i++) {
            for (int j = 0; j < inputArray[i].length; j++) {
                int result = random.nextInt(2);
                System.out.print(result+"\t");
            }
            System.out.println();
        }
    }
}

“int result = random.nextInt(2);” means that the value limited to binary ( is not more than 2). And below the output :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s