Results 1 to 12 of 12
  1. #1
    AnimeKitty's Avatar
    AnimeKitty is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default 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 [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);
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

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

    Java 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

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,319
    Rep Power
    25

    Default

    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.

  4. #4
    AnimeKitty's Avatar
    AnimeKitty is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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);
        }
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,319
    Rep Power
    25

    Default

    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?

  6. #6
    AnimeKitty's Avatar
    AnimeKitty is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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?
    Sorry about the delay.
    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.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);
          	  		}
          		}
        	}
            }
    }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,319
    Rep Power
    25

    Default

    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]));
    Last edited by Norm; 07-30-2010 at 03:16 AM.

  8. #8
    AnimeKitty's Avatar
    AnimeKitty is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  11. #11
    AnimeKitty's Avatar
    AnimeKitty is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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!

    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);
          	  		}
          		}
        	}
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.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();
             }
          });
       }
    }

Similar Threads

  1. 48 bits JPEG 2000
    By user_java in forum Java 2D
    Replies: 0
    Last Post: 03-02-2010, 05:19 AM
  2. Image to Binary bits
    By Deva in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 04:49 PM
  3. extracting bits from big numbers
    By ankitmcgill in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 04:36 AM
  4. 2D Array Random Shuffle
    By Nuluvius in forum New To Java
    Replies: 8
    Last Post: 11-23-2008, 06:15 PM
  5. How to get a random value in an array
    By Franneldort in forum New To Java
    Replies: 21
    Last Post: 11-01-2008, 02:42 PM

Posting Permissions

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