# Help with random bits array

• 07-26-2010, 10:50 AM
AnimeKitty
Help with random bits array
I wound like to create an array with random bits instead of integers.

Code:

```int image1[][]; int sum[][]; sum = new int [50][50]; image1 = new int [50][50]; for(x=0; x<50; x++){     for(y=0; y<50; y++){         image1[x][y] = (int) (Math.random() *(1);     } }```
• 07-26-2010, 12:58 PM
JosAH
Quote:

Originally Posted by AnimeKitty
I wound like to create an array with random bits instead of integers.

Code:

```int image1[][]; int sum[][]; sum = new int [50][50]; image1 = new int [50][50]; for(x=0; x<50; x++){     for(y=0; y<50; y++){         image1[x][y] = (int) (Math.random() *(1);     } }```

The Random.nextInt(2) method call gives you (pseudo) random numbers 0 or 1.

kind regards,

Jos
• 07-26-2010, 01:51 PM
Norm
Quote:

with random bits
Not sure what your restrictions are here.
Your code sort of implies that you want random integers of value 0 or 1 only.

Since everything in a computer is composed of bits (8 to a byte, etc) then any random integer will be made up of random bits.
Can you explain your problem more.
• 07-26-2010, 04:22 PM
AnimeKitty
Quote:

Originally Posted by Norm
Not sure what your restrictions are here.
Your code sort of implies that you want random integers of value 0 or 1 only.

Since everything in a computer is composed of bits (8 to a byte, etc) then any random integer will be made up of random bits.
Can you explain your problem more.

What I trying to do is generate random 1s and 0s the simplest way possible.
Integers are to big. Are bytes the smallest units that can be used to generate random numbers.

I'm not sure how to make random numbers with bytes.

Code:

```import java.util.Random; Random number = new Random(byte);                 for(int x=0; x>=50; x++){     for(int y=0; y>=50; y++){         image1[x][y] = number.nextBytes(1);     } }```
• 07-26-2010, 04:27 PM
Norm
Make random ints and cast them to bytes

Read the API doc for the Random class. The nextBytes() method will fill an array.

What are you using a 2 dim array of 0s and 1s for?
• 07-30-2010, 03:04 AM
AnimeKitty
Quote:

Originally Posted by Norm
Make random ints and cast them to bytes

Read the API doc for the Random class. The nextBytes() method will fill an array.

What are you using a 2 dim array of 0s and 1s for?

I just found out about boolean and how it can be true or false (1 and 0).
I would like to make white noise in a JFrame as a starting point for a cellular automata program I'm working on.

This is the code I have now but it seams not to work. :confused:

Code:

```import java.util.Random; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Noise2 extends JPanel {   public static void main(String[] args) {     JFrame f = new JFrame();     f.setSize(400, 400);     f.add(new Noise2());     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     f.setVisible(true);   }   public void paint(Graphics g) {             Graphics2D graphics = (Graphics2D) g;             Dimension size = getSize();             boolean [][] a = new boolean [size.width][size.height];             for (int x = 0; x > size.width; x++) {                       for (int y = 0; y > size.height; y++) {                               int r = (int) Math.random() * 10;                               if(r>5){                                       a[x][y] = true;                               }                                 if(r<=5){                                       a[x][y] = false;                               }                       }             }             for (int x = 0; x > size.width; x++) {                       for (int y = 0; y > size.height; y++) {                                 if(a[x][y] = true){                                         Color c = new Color(255, 255, 255);                                         graphics.setColor(c);                                   graphics.fillRect(x, y, 1, 1);                                 }                                 if(a[x][y] = false){                                         Color c = new Color(0, 0, 0);                                         graphics.setColor(c);                                   graphics.fillRect(x, y, 1, 1);                                 }                       }             }         } }```
• 07-30-2010, 03:09 AM
Norm
Quote:

it seems not to work
Can you describe what does not work? What does it do that you don't like?
What would you like it to do?

Do some debugging by adding println() statements to the code to see what is happening.
For example, add this after a is filled:

System.out.println(Arrays.toString(a[0]) + "\n" + Arrays.toString(a[1]));
• 07-30-2010, 03:16 AM
AnimeKitty
Quote:

Originally Posted by Norm
Can you describe what does not work? What does it do that you don't like?
What would you like it to do?

When I run the program I get a blank JFrame.
I would like it to display random black and white pixels inside of the JFrame.
• 07-30-2010, 03:37 AM
Fubarable
Norm and I know what's going wrong, but he is trying to get you to discover it yourself. You may wish to do the debugging that he has suggested.

Luck
• 07-30-2010, 04:23 AM
Fubarable
But regardless of misused greater than and less than signs in for loops, perhaps you should draw your random bits directly into a BufferedImage, perhaps one that uses BufferedImage.TYPE_BYTE_BINARY for the imageType.
• 07-30-2010, 04:25 AM
AnimeKitty
Quote:

Originally Posted by Norm
Do some debugging by adding println() statements to the code to see what is happening.
For example, add this after a is filled:

System.out.println(Arrays.toString(a[0]) + "\n" + Arrays.toString(a[1]));

That helped allot, thanks!

Code:

```public void paint(Graphics g) {             Graphics2D graphics = (Graphics2D) g;             Dimension size = getSize();             boolean a[][] = new boolean [size.width][size.height];                 Random r = new Random();             for (int x = 0; x < size.width; x++) {                       for (int y = 0; y < size.height; y++) {                               boolean b = r.nextBoolean();                               a[x][y] = b;                       }             }             for (int x = 0; x < size.width; x++) {                       for (int y = 0; y < size.height; y++) {                                 if(a[x][y] == true){                                         Color c = new Color(255, 255, 255);                                         graphics.setColor(c);                                   graphics.fillRect(x, y, 1, 1);                                 }                                 if(a[x][y] == false){                                         Color c = new Color(0, 0, 0);                                         graphics.setColor(c);                                   graphics.fillRect(x, y, 1, 1);                                 }                       }             } }```
• 07-30-2010, 04:37 AM
Fubarable
Example with a BufferedImage:
Code:

```import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.util.Random; import javax.swing.*; @SuppressWarnings("serial") public class Noise2b extends JPanel {   public static final int WHITE = Color.white.getRGB();   public static final int BLACK = Color.black.getRGB();   private static final Dimension PREF_SIZE = new Dimension(600, 400);   private BufferedImage bImage;   private Random random = new Random();   public Noise2b() {       setPreferredSize(PREF_SIZE);       Dimension scrnSz = Toolkit.getDefaultToolkit().getScreenSize();       bImage = new BufferedImage(scrnSz.width, scrnSz.height, BufferedImage.TYPE_BYTE_BINARY);       for (int x = 0; x < scrnSz.width; x++) {         for (int y = 0; y < scrnSz.height; y++) {             int c = random.nextBoolean() ? WHITE : BLACK;             bImage.setRGB(x, y, c);         }       }   }   @Override   protected void paintComponent(Graphics g) {       super.paintComponent(g);       if (bImage != null) {         g.drawImage(bImage, 0, 0, null);       }   }   private static void createAndShowUI() {       JPanel mainPanel = new Noise2b();       JFrame frame = new JFrame("Noise over Noise2b");       frame.getContentPane().add(mainPanel);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.pack();       frame.setLocationRelativeTo(null);       frame.setVisible(true);   }   public static void main(String[] args) {       java.awt.EventQueue.invokeLater(new Runnable() {         public void run() {             createAndShowUI();         }       });   } }```