# Thread: Help with random bits array

1. ## Help with random bits array

I wound like to create an array with random bits instead of integers.

Java Code:
```int image1[][];
int sum[][];
sum = new int ;
image1 = new int ;

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

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

Java Code:
```int image1[][];
int sum[][];
sum = new int ;
image1 = new int ;

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  Reply With Quote

3. ## 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.  Reply With Quote

4. ##  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.

Java 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);
}
}```  Reply With Quote

5. ## 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?  Reply With Quote

6. ##  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:

Java 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.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);
}
}
}
}
}```  Reply With Quote

7. ## 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) + "\n" + Arrays.toString(a));
Last edited by Norm; 07-30-2010 at 03:16 AM.  Reply With Quote

8. ##  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.  Reply With Quote

9. ## 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  Reply With Quote

10. ## 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.  Reply With Quote

11. ##  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) + "\n" + Arrays.toString(a));
That helped allot, thanks!

Java 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);
}
}
}
}```  Reply With Quote

12. ## Example with a BufferedImage:
Java 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.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();
}
});
}
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•